okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
44d885874
main
1import { fileURLToPath } from 'node:url' ; 2import assert from 'node:assert/strict' ; 3import { test } from 'node:test' ; 4import { readFile } from 'node:fs/promises' ; 5import { build } from 'esbuild' ; 6const result = await build ({ entryPoints :[ fileURLToPath ( new URL ( '../src/lib/experience.ts' , import . meta. url ))], bundle :true , write :false , platform :'node' , format :'esm' }); 7const code = result . outputFiles [ 0 ]. text ; 8const { estimate, recommend, configKey, hardwareKey, groupVariants, DEFAULT_EXECUTION :config } = await import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 9const hw = { cpu :{ model :'Fixture CPU' }, gpus :[{ name :'Fixture GPU' }], memory :{ totalGb :32 }, accelerator :{ unified :true , backend :'metal' , usableVramGb :24 } }; 10const base = { id :'fixture-q4' , name :'Fixture' , family :'Fixture-8B' , params :'8B' , quant :'Q4' , diskGb :4 , kvKbPerToken :24 , experts :0 , contextMax :32768 , catalog :{ family :'Fixture' , version :'8B' , purpose :'Test' , capabilities :[ 'Text' ], agentApproved :false , balancedRank :2 , review :'Not reviewed' } }; 11function measured ( v , reading , answer , extra = {}) { return { ...v , performance :[{ source :'measured' , hardware :hardwareKey ( hw ), backend :'metal' , config :configKey ( v , config ), workload :'document' , cache :'cold-prompt' , reading :{ low :reading , high :reading + 10 }, answer :{ low :answer , high :answer + 5 }, date :'2026-10-05' , attribution :'Test fixture' , ...extra }] }; } 12test ( 'unknown hardware and invalid drafts do not manufacture fit or performance' , () => { 13const unknown = estimate ( base , null , config , 'document' , false ); 14assert . equal ( unknown . fit , 'unknown' ); assert . equal ( unknown . reading , null ); assert . equal ( unknown . memory . budget , null ); 15const invalid = estimate ( base , hw , { ...config , batch :NaN }, 'document' , true ); 16assert . equal ( invalid . memory , null ); assert . equal ( invalid . answer , null ); 17assert . ok ( recommend ([ base ], null , config , 'document' , true ). every ( r => ! r . variant )); 18}); 19test ( 'context changes memory; unified pools are never doubled; offload remains unknown' , () => { 20const a = estimate ( base , hw , config ), b = estimate ( base , hw , { ...config , context :8192 }); 21assert . ok ( a . memory . cache > b . memory . cache ); assert . equal ( a . memory . total , a . memory . weights + a . memory . cache + a . memory . runtime ); 22assert . equal ( a . memory . systemBudget , null ); assert . equal ( a . memory . budget , 32 * .72 ); 23const discrete = { ...hw , accelerator :{ unified :false , backend :'cuda' , usableVramGb :8 } }; 24assert . equal ( estimate ( base , discrete , config ). memory . budget , 8 ); 25assert . equal ( estimate ( base , discrete , { ...config , placement :'Half' }). fit , 'unknown' ); 26assert . equal ( estimate ( base , hw , { ...config , context :4096 }, 'document' ). fitsWorkload , false ); 27assert . equal ( estimate ( base , hw , { ...config , context :4096 }, 'document' , true ). reading , null ); 28assert . equal ( estimate ( base , hw , { ...config , context :65536 }). fit , 'unsupported' ); 29}); 30test ( 'agent capability gating beats raw reading speed; output speed selects Performance' , () => { 31const fast = measured ({ ...base , id :'fast' }, 900 , 150 ); 32const agent = measured ({ ...base , id :'agent' , catalog :{ ...base . catalog , agentApproved :true , balancedRank :1 , review :'Reviewed fixture' } }, 400 , 40 ); 33const r = recommend ([ fast , agent ], hw , config , 'document' , false ); 34assert . equal ( r . find ( x => x . profile === 'agentic' ). variant . id , 'agent' ); 35assert . equal ( r . find ( x => x . profile === 'performance' ). variant . id , 'fast' ); 36assert . equal ( r . find ( x => x . profile === 'balanced' ). variant . id , 'agent' ); 37assert . equal ( recommend ([ fast ], hw , config , 'document' , false )[ 0 ]. variant , undefined ); 38}); 39test ( 'unmatched evidence cannot rank a changed configuration or be presented as measured' , () => { 40const v = measured ( base , 300 , 40 ); 41assert . equal ( estimate ( v , hw , config ). source , 'measured' ); 42assert . equal ( estimate ( v , hw , { ...config , batch :4096 }). source , 'unavailable' ); 43assert . equal ( estimate ( v , hw , config , 'short' ). source , 'unavailable' ); 44const wrongBackend = measured ( base , 300 , 40 , { backend :'cuda' }); 45assert . equal ( estimate ( wrongBackend , hw , config ). source , 'unavailable' ); 46const demo = estimate ( wrongBackend , hw , config , 'document' , true ); 47assert . equal ( demo . source , 'demo' ); assert . match ( demo . provenance , / not a measurement / ); 48assert . equal ( wrongBackend . performance . length , 1 ); 49}); 50test ( 'missing speed is provisional; the same approved variant may win all three profiles' , () => { 51assert . ok ( recommend ([ base ], hw , config , 'document' , false ). every ( x => x . provisional )); 52const approved = measured ({ ...base , catalog :{ ...base . catalog , agentApproved :true , review :'Reviewed fixture' } }, 300 , 80 ); 53assert . deepEqual ( recommend ([ approved ], hw , config , 'document' , false ). map ( x => x . variant . id ), [ base . id , base . id , base . id ]); 54assert . ok ( recommend ([{ ...base , diskGb :100 }], hw , config , 'document' , false ). every ( x => ! x . variant )); 55}); 56test ( 'large catalogs preserve every exact variant within stable family/version groups' , () => { 57const variants = Array . from ({ length :3000 }, ( _ , i ) => ({ ...base , id :`variant- ${ i } ` , quant :`quant- ${ i % 20 } ` , catalog :{ ...base . catalog , family :`Family ${ Math . floor ( i / 100 )} ` , version :`Release ${ Math . floor ( i / 20 )} ` } })); 58const groups = groupVariants ( variants ); 59assert . equal ( groups . length , 30 ); assert . equal ( groups [ 0 ]. versions . length , 5 ); 60assert . equal ( groups . flatMap ( g => g . versions . flatMap ( v => v . variants )). length , 3000 ); 61}); 62 63test ( 'shared winners merge by variant AND full execution configuration' , async () => { 64const { groupRecommendations} = await import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 65const r = recommend ([ base ], hw , config , 'document' , false ); 66assert . equal ( groupRecommendations ( r ). length , 2 ); // One absent Agentic, one shared winner. 67const changed = r . map ( x => x . profile === 'performance' ?{ ...x , experience :estimate ( base , hw , { ...config , batch :1024 }) } :x ); 68assert . equal ( groupRecommendations ( changed ). length , 3 ); 69for ( const patch of [{ context :8192 }, { kv :'FP16' }, { placement :'CPU only' }, { threads :4 }]) { 70assert . equal ( groupRecommendations ( r . map ( x => x . profile === 'performance' ?{ ...x , experience :estimate ( base , hw , { ...config , ...patch }) } :x )). length , 3 ); 71} 72});