okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// Enumerate proposal and acceptance branches exactly; no Monte Carlo or GPU. 2#include "strata/core/spec_prob.hpp" 3#include "strata/core/gated_proposal.hpp" 4#include "strata/spec/draft_policy.hpp" 5#include <algorithm> 6#include <array> 7#include <cmath> 8#include <cstdio> 9#include <cstring> 10using namespace strata::core ; 11using Dist = std::array < double ,2 > ; 12int failures = 0 ; 13void check (bool ok ,const char * message ) { 14if (!ok ) {++ failures ; std::fprintf (stderr ,"FAIL: %s\n" ,message ); } 15} 16Dist judged (const Dist & p ,const std::array < float ,2 >& q ,int d ) { 17const int32_t ids [2 ]= {0 ,1 }; 18const SpecP P {ids ,p .data (),2 };const SpecQ Q {ids ,q .data (),2 }; 19const double a = std::min (1.0 ,p [d ]/q [d ]);Dist result {}; 20if (a > 0 )result [spec_verify_row (P ,d ,Q ,(float )(a /2 ),0.5f ).token ]+= a ; 21if (a < 1 )result [spec_verify_row (P ,d ,Q ,(float )((1 + a )/2 ),0.5f ).token ]+= 1 - a ; 22return result ; 23} 24Dist legacy_raw_q_plain_p (const Dist & p ,const std::array < float ,2 >& q ,bool own_draw ,float threshold ) { 25Dist result {}; 26for (int d = 0 ;d < 2 ;++ d ) { 27const float gate = own_draw ?q [d ]:std::max (q [0 ],q [1 ]); 28const auto row = gate >=threshold ?judged (p ,q ,d ):p ; 29for (int i = 0 ;i < 2 ;++ i )result [i ]+= q [d ]* row [i ]; 30 } 31return result ; 32} 33bool equal (const Dist & a ,const Dist & b ) { 34return std::abs (a [0 ]- b [0 ])< 1e-12 && std::abs (a [1 ]- b [1 ])< 1e-12 ; 35} 36// Enumerate the repaired first-row mixture using the ACTUAL packet builder. 37// No candidate-conditioned renormalization: every draw receives the same root mu. 38Dist proposal_contract (const Dist & p ,const std::array < float ,2 >& q ,float threshold , 39 strata::core::gated::RootChoice root = {}) { 40std::array < int32_t,strata::core::gated::kStride > raw ; 41raw . fill ( -1 ); raw [ 0 ] = 0 ; raw [ 1 ] = 1 ; 42std:: memcpy ( raw . data () + strata::core::gated:: kEntries , q . data (), sizeof ( q )); 43Dist result {}; 44for (int d = 0 ; d < 2 ; ++ d ) { 45const int32_t draft = d ; const float score = q [ d ];strata::core::gated::Packet packet ; 46const bool ok = strata::core::gated:: make_packet ( packet , raw . data (), & draft , & score , 1 , 1 , 2 , 2 , threshold ,false, root ); 47check ( ok , "valid gated packet" ); if (! ok ) continue ; 48Dist mu {strata::core::gated:: mass_at ( packet . mu . data (), 0 ),strata::core::gated:: mass_at ( packet . mu . data (), 1 )}; 49double acceptance = 0 ; 50if (! packet . null_tail ) { 51check ( mu [ d ] > 0 , "offer has positive mu" ); 52acceptance = std:: min ( 1.0 , p [ d ]/ mu [ d ]); 53result [ d ] += q [ d ] * acceptance ; 54} 55if ( acceptance < 1 ) { 56Dist residual {std:: max ( 0.0 , p [ 0 ] - mu [ 0 ]),std:: max ( 0.0 , p [ 1 ] - mu [ 1 ])}; 57const double sum = residual [ 0 ] + residual [ 1 ]; check ( sum > 0 , "reachable failure has residual" ); 58if ( sum > 0 ) for (int y = 0 ; y < 2 ; ++ y ) result [ y ] += q [ d ] * ( 1 - acceptance ) * residual [ y ]/ sum ; 59} 60} 61return result ; 62} 63int main (int argc ,char ** argv ) { 64const bool expect_pick = !( argc > 1 && std:: strcmp ( argv [ 1 ], "top" ) == 0 ); 65check ( spec_gate_pick () == expect_pick , "pick is default; only explicit lowercase top selects Top compatibility" ); 66const Dist p { 0.5 , 0.5 }; const std::array < float, 2 > q { 0.875f , 0.125f }; 67check ( equal ( legacy_raw_q_plain_p ( p , q ,true, 0.5f ),Dist{ 0.5625 , 0.4375 }), 68"LEGACY negative control: own-draw gate with full-q verifier/plain-p failure is biased" ); 69check ( equal ( proposal_contract ( p , q , 0.5f ), p ), "new pick mu/NULL mixture preserves p" ); 70check ( equal ( spec_gate_pick ()? proposal_contract ( p , q , 0.5f ): legacy_raw_q_plain_p ( p , q ,false, 0.5f ), p ), 71"configured repaired pick or Top compatibility preserves p" ); 72int top_cases = 0 , proposal_cases = 0 ; 73for (int a = 1 ; a < 16 ; ++ a ) for (int b = 1 ; b < 16 ; ++ b ) { 74const Dist P { a / 16.0 , 1 - a / 16.0 }; const std::array < float, 2 > Q { b / 16.0f , 1 - b / 16.0f }; 75for (float threshold :{ 0.0f , 0.5f , 0.9f , 1.1f }) { 76check ( equal ( legacy_raw_q_plain_p ( P , Q ,false, threshold ), P ), "Top compatibility distribution law" ); ++ top_cases ; 77check ( equal ( proposal_contract ( P , Q , threshold ), P ), "H-only proposal mixture" ); ++ proposal_cases ; 78for (int rescue :{ 0 , 1 }) { 79check ( equal ( proposal_contract ( P , Q , threshold ,{true, rescue , 2 }), P ), "fixed root UNION on every source branch" ); ++ proposal_cases ; 80} 81} 82} 83// A planner must not learn from draws it may select, including discarded position-keyed lookahead. 84strata::spec::DraftPolicy live ( 6 ); 85for (int i = 0 ; i < 5 ; ++ i ) { live . observe (false, 4 , 3 , 24 , 1.0 ); live . observe (false, 6 , 5 , 24 , 1.5 ); } 86const auto request_plan = live ; 87auto short_chain = live , long_chain = live ; 88short_chain . observe (false, 4 , 3 , 24 , 0.2 ); long_chain . observe (false, 4 , 3 , 24 , 8.0 ); 89const bool short_lookup = short_chain . choose ( 4 , 5 , 24 ). lookup , long_lookup = long_chain . choose ( 4 , 5 , 24 ). lookup ; 90check (! short_lookup && long_lookup , "draw-dependent chain time can change the actual policy choice" ); 91Dist conditional {}, planned {}; 92for (int d = 0 ; d < 2 ; ++ d ) { 93const auto selected = ( d == 0 ? long_lookup : short_lookup )? p : judged ( p , q , d ); 94const auto fixed = request_plan . choose ( 4 , 5 , 24 ). lookup ? p : judged ( p , q , d ); 95for (int i = 0 ; i < 2 ; ++ i ) { conditional [ i ] += q [ d ] * selected [ i ]; planned [ i ] += q [ d ] * fixed [ i ]; } 96} 97check ( equal ( conditional ,Dist{ 0.4375 , 0.5625 }), "LEGACY negative: D-conditioned source without common first measure changes proposal law" ); 98check ( equal ( planned , p ), "request plan preserves p despite subsequent timing observations" ); 99std:: printf ( "spec_admission_test: %d Top compatibility cases, %d actual mu-packet cases, legacy negatives; %d failures\n" , top_cases , proposal_cases , failures ); 100return failures ? 1 : 0 ; 101}