okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01d0646fd
main
1// (asserts stay on in a Release build: the test is its own check) 2#undef NDEBUG 3#include "strata/program/reply_branch.hpp" 4 5#include <cassert> 6#include <cstdint> 7#include <iostream> 8#include <vector> 9 10using strata::program::live_tail_is_replaced_reply ; 11 12namespace { 13constexpr int64_t S = 900 ;// <|im_start|> 14constexpr int64_t E = 901 ;// <|im_end|> 15constexpr int64_t SYS = 10 ,USER = 11 ,ASSIST = 12 ; 16 17std::vector < int64_t > cat (std::initializer_list < std::vector < int64_t >> parts ) { 18 std::vector < int64_t > v ; 19for (const auto & p :parts )v .insert (v .end (),p .begin (),p .end ()); 20return v ; 21} 22std::vector < int32_t > to32 (const std::vector < int64_t >& v ) {return std::vector < int32_t > (v .begin (),v .end ()); } 23}// namespace 24 25int main () { 26const std::vector < int64_t > sys = {S ,SYS ,1 ,2 ,3 ,E }; 27const std::vector < int64_t > user1 = {S ,USER ,4 ,5 ,6 ,E }; 28const std::vector < int64_t > head = {S ,ASSIST ,7 };// the generation header (<|im_start|>assistant\n...) 29const std::vector < int64_t > reasoning = {20 ,21 ,22 ,23 }; 30const std::vector < int64_t > answer = {30 ,31 ,32 }; 31const std::vector < int64_t > user2 = {S ,USER ,8 ,9 ,E }; 32const auto history = cat ({sys ,user1 }); 33const int64_t turn = static_cast < int64_t > (history .size ());// the turn checkpoint: [0, turn) 34// live: the last request's prompt + its reply (reasoning and answer), the last emitted token not fed 35const auto live = to32 (cat ({history ,head ,reasoning ,answer })); 36int cases = 0 ; 37 38// (b) reasoning dropped: the history renders the assistant turn with the answer only, then the next user turn 39assert (live_tail_is_replaced_reply (live ,cat ({history , {S ,ASSIST },answer , {E },user2 ,head }),turn ,S ));++ cases ; 40// (c) one word of the answer changed 41assert (live_tail_is_replaced_reply (live ,cat ({history , {S ,ASSIST }, {30 ,99 ,32 }, {E },user2 ,head }),turn ,S ));++ cases ; 42// regenerate: the same history again, a new assistant turn opens where the dropped one was 43assert (live_tail_is_replaced_reply (live ,cat ({history ,head }),turn ,S ));++ cases ; 44// the live branch was only a prompt read (prefill), nothing generated past the header 45assert (live_tail_is_replaced_reply (to32 (cat ({history ,head })),cat ({history , {S ,ASSIST },answer , {E },user2 ,head }),turn , 46S ));++ cases ; 47 48// a new USER turn where the reply was (the client left the reply out entirely): another kind of request, park as before 49assert (!live_tail_is_replaced_reply (live ,cat ({history ,user2 ,head }),turn ,S ));++ cases ; 50// the live tail holds more than one turn (a tool result, a user turn after the reply): not just one reply 51assert (!live_tail_is_replaced_reply (to32 (cat ({history ,head ,answer , {E },user2 ,head ,answer })), 52cat ({history , {S ,ASSIST },answer , {E },user2 ,head }),turn ,S ));++ cases ; 53// resume not at a turn opening of the live branch (a periodic or root checkpoint in the middle of a message) 54assert (!live_tail_is_replaced_reply (live ,cat ({history ,head }),turn - 2 ,S ));++ cases ; 55// the prompt differs before resume (another conversation): never 56 { 57auto other = cat ({history ,head }); 58other [2 ]= 77 ; 59assert (!live_tail_is_replaced_reply (live ,other ,turn ,S ));++ cases ; 60 } 61// the live branch ends at the checkpoint (nothing past it) or one token past it: nothing to say about a reply 62assert (!live_tail_is_replaced_reply (to32 (history ),cat ({history ,head }),turn ,S ));++ cases ; 63assert (!live_tail_is_replaced_reply (to32 (cat ({history , {S }})),cat ({history ,head }),turn ,S ));++ cases ; 64// the prompt ends right at the turn opening (one token past resume): no role to compare 65assert (!live_tail_is_replaced_reply (live ,cat ({history , {S }}),turn ,S ));++ cases ; 66// picture_at_or_past_checkpoint_parks: the live branch has a picture at or past resume: parked as before 67assert (!live_tail_is_replaced_reply (live ,cat ({history ,head }),turn ,S , {turn }));++ cases ; 68assert (!live_tail_is_replaced_reply (live ,cat ({history ,head }),turn ,S , {2 ,turn + 3 }));++ cases ; 69// a picture before the checkpoint (in the reused history) does not stop the rule 70assert (live_tail_is_replaced_reply (live ,cat ({history ,head }),turn ,S , {2 }));++ cases ; 71// no turn token configured, resume 0 72assert (!live_tail_is_replaced_reply (live ,cat ({history ,head }),turn ,-1 ));++ cases ; 73assert (!live_tail_is_replaced_reply (live ,cat ({history ,head }),0 ,S ));++ cases ; 74 std::cout <<"SUMMARY reply_branch cases=" <<cases <<" passed\n" ; 75return 0 ; 76}