okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
1/** 2* Lane APP-LIN (0.2.14) item 7: on a card where Control's Auto means the MTP (here a 12 GB CUDA card, CLASS_DEFAULTS "streaming"), the app's setup prepares the MTP 3* layer and the first load after it drafts. One decider: the install asks Control's `drafting plan` (strata_control/drafting.py), not the host's own rule. 4* The fixture is split-app.test.mjs's (fake Control, fake installer over MCP) with one 12 GB card. Generated by the lane's script; edit freely. 5*/ 6import './helpers/isolate-profile.mjs' ; // a test run never writes the owner's real Strata profile 7import test from 'node:test' ; 8import assert from 'node:assert/strict' ; 9import { EventEmitter } from 'node:events' ; 10import { mkdtempSync , mkdirSync , readFileSync , rmSync , writeFileSync } from 'node:fs' ; 11import { join , dirname } from 'node:path' ; 12import { tmpdir } from 'node:os' ; 13import { createNativeControl } from '../server/native-control.mjs' ; 14 15function fixture ({ plan, split= null , installFails= false , startFails= 0 , metrics= {}, config= {} } = {}) { 16const root = mkdtempSync ( join ( tmpdir (), 'strata-applin-drafter-' )); 17const install = join ( root , 'install' ), data = join ( root , 'host-data' ), home = join ( root , 'home' ), modelsDir = join ( home , 'models' ); 18for ( const d of [ install , data , modelsDir ]) mkdirSync ( d , { recursive :true }); 19const earlier = process . env . STRATA_INSTALL_DIR ; 20process . env . STRATA_INSTALL_DIR = install ; 21const dataFolder = join ( root , 'Strata-data' ), modelFolder = join ( dataFolder , 'models' , 'UD-Q4_K_XL' ); 22const gguf = join ( modelFolder , 'Qwen3.8-Flash-Next-UD-Q4_K_XL-00001-of-00002.gguf' ); 23mkdirSync ( modelFolder , { recursive :true }); 24writeFileSync ( gguf , 'GGUF' ); writeFileSync ( gguf . replace ( '00001' , '00002' ), 'GGUF' ); 25const pack = join ( dataFolder , 'packs' , 'ud_q4_k_xl' ), mtp = join ( dataFolder , 'mtp' , 'rt' ); 26mkdirSync ( join ( pack , 'tokenizer' ), { recursive :true }); 27writeFileSync ( join ( pack , 'native_experts.txt' ), 'x' ); 28const configPath = join ( install , 'strata-ud_q4_k_xl.json' ); 29const args = [ '--pack' , pack , '--native' , gguf , '--ple-gguf' , gguf . replace ( '00001' , '00002' ), '--mtp' , mtp , '--max-context' , '131072' , '--kv' , 'int8' ]; 30writeFileSync ( configPath , JSON . stringify ({ exe :'component:strata-engine' , tokenizer :join ( pack , 'tokenizer' ), args, model_name :'qwen3.8-flash-next-ud_q4_k_xl' , port :8080 , ...config })); 31writeFileSync ( process . env . STRATA_SETTINGS_FILE , JSON . stringify ({ data_dir :join ( root , 'host-owned' , 'Strata-data' ), data_dir_at :1700000000 })); 32const state = { installs :0 , planCalls :[], fetchCalls :[], splitCalls :[], saved :[], starts :0 , rpc :[] }; 33const control = { 34control :{ dir :join ( root , 'control' ), python :join ( root , 'control' , 'python' , 'python' ) }, version :'0.2.10' , 35async installedModels () { 36return { install_dir :install , models :[{ id :'ud_q4_k_xl' , name :'qwen3.8-flash-next-ud_q4_k_xl' , source_config :configPath , context_tokens :131072 , backend :'cuda' , component_status :'present' , missing :[], issue_codes :[] }] }; 37}, 38async engine () { return { usable :true , backend :'cuda' , components_dir :join ( root , 'c' ), engine :{ version :'0.1.56' , platform :'linux-x86_64' , backend :'cuda' , exe :join ( root , 'engine' , 'strata' ), root :join ( root , 'engine' ) } }; }, 39async draftingPlan ( options ) { state . planCalls . push ( options ); return typeof plan === 'function' ?plan ( options , state ) :plan ; }, 40async draftingFetch ( options ) { state . fetchCalls . push ( options ); return { ok :true , event :'result' , pack :null , skipped :false }; }, 41async splitEngine ( options ) { 42state . splitCalls . push ({ ...options , input :JSON . parse ( readFileSync ( options . configPath , 'utf8' )) }); 43if ( ! split ) return { ok :true , split :false , line :'' , warn :false , reasons :[], written :null }; 44return split ( options , state ); 45}, 46async saveProfile ( name , path ) { state . saved . push ( JSON . parse ( readFileSync ( path , 'utf8' ))); return {}; }, 47async start () { state . starts += 1 ; if ( state . starts <= startFails ) throw new Error ( 'The engine stopped: out of memory' ); return { ok :true , state :'ready' }; }, 48async stop () { return { ok :true }; }, 49}; 50const catalog = { families :[{ family :'qwen' , title :'Qwen3.8-Flash-Next' , about :'x' , images :false , experimental :false , sizes :[ 51{ id :'ud_q4_k_xl' , model :'UD-Q4_K_XL' , download_gb :22 , ram_needed_gb :60 , about :'x' , on_this_pc :'fits' , installed :true , images :false , experimental :false }] }], 52contexts :[ 32768 , 131072 ], recommendation :{ family :'qwen' , model :'UD-Q4_K_XL' , context :131072 }, context_note :'' }; 53const hardware = { ram_gb :256 , cpu :{ name :'cpu' , cores :32 }, engine_vendor :'nvidia' , gpus :[{ index :0 , name :'NVIDIA GeForce RTX 3060' , vendor :'nvidia' , vram_gb :12 , arch :86 , usable :true }] }; 54const spawnProcess = ( command , argv ) => { 55if ( ! String ( argv [ 0 ]). endsWith ( 'strata_mcp.py' )) throw new Error ( 'no process but the installer may be started in packaged mode: ' + command + ' ' + argv . join ( ' ' )); 56const child = new EventEmitter (); child . stdout = new EventEmitter (); child . stderr = new EventEmitter (); child . exitCode = null ; 57child . kill = () => { child . exitCode = 0 ; queueMicrotask (() => child . emit ( 'exit' , 0 )); return true ; }; 58child . stdin = { write ( line ) { 59const message = JSON . parse ( line ); if ( ! message . id ) return ; 60if ( message . method === 'tools/call' ) state . rpc . push ( message . params ); 61let result ; 62if ( message . method === 'initialize' ) result = { protocolVersion :'2024-11-05' }; 63else if ( message . method === 'tools/list' ) result = { tools :[ 'strata_models' , 'strata_status' , 'strata_install' , 'strata_remove' ]. map ( name => ({ name})) }; 64else if ( message . params . name === 'strata_models' ) result = { structuredContent :catalog }; 65else if ( message . params . name === 'strata_status' ) result = { structuredContent :{ hardware, recommendation :catalog . recommendation , install :null , running :[], summary :'x' } }; 66else if ( Object . keys ( message . params . arguments ?? {}). length === 0 ) result = { structuredContent :{ summary :'Install finished' , install :{ running :false , exit_code :0 } } }; 67else if ( message . params . arguments ?. confirm ) { 68if ( installFails ) result = { isError :true , structuredContent :{ error :'The download did not finish.' } }; 69else { state . installs += 1 ; mkdirSync ( mtp , { recursive :true }); writeFileSync ( join ( mtp , 'dense.bin' ), 'x' ); result = { structuredContent :{ summary :'Install started' , plan :{}, install :{ running :true , step :'1/7' } } }; } 70} else result = { structuredContent :{ summary :'Plan' , plan :{ title :'Qwen3.8-Flash-Next UD-Q4_K_XL' , download_gb :5.2 , disk_short :null } } }; 71queueMicrotask (() => child . stdout . emit ( 'data' , Buffer . from ( JSON . stringify ({ jsonrpc :'2.0' , id :message . id , result}) + String . fromCharCode ( 10 )))); 72} }; 73return child ; 74}; 75const fetcher = async url => String ( url ). endsWith ( '/metrics' ) ?Response . json ({ engine :metrics }) :Response . json ({ status :'ok' }); 76const native = createNativeControl ({ control, directory :data , spawnProcess, fetcher, requests :() => [], modelsDirectory :() => modelsDir , hardwareProbe :() => [{ index :0 , vram_gb :12 }] }); 77return { native, state, root, gguf, modelFolder, mtp, configPath, args, 78cleanup :() => { if ( earlier === undefined ) delete process . env . STRATA_INSTALL_DIR ; else process . env . STRATA_INSTALL_DIR = earlier ; rmSync ( root , { recursive :true , force :true }); } }; 79} 80const finish = async ( native , id ) => { for ( let i = 0 ; i < 1500 ; i ++ ) { const job = native . state (). jobs . find ( j => j . id === id ); if ( job . state !== 'running' ) return job ; await new Promise ( r => setTimeout ( r , 10 )); } throw new Error ( 'job did not finish' ); }; 81const load = ( f , args = {}) => f . native . operation ( 'load' , { model :'ud_q4_k_xl' , ...args }); 82 83/** Control's plan: at the install (no run config yet) `atInstall`; at the load the MTP layer is missing until `layerAfter` installs ran. `serves`: DFlash2 is served here. */ 84const planner = ({ atInstall, layerAfter= 1 }) => ( options , state ) => { 85if ( ! options . configPath && options . family === undefined ) return { ok :true , serves :true , choice :'dflash2' , split :false }; // the start-up probe (mode auto) 86if ( ! options . configPath ) { if ( atInstall instanceof Error ) throw atInstall ; return { ok :true , serves :true , split :false , needed :false , mtp_needed :false , gb :0 , ...atInstall }; } 87const missing = state . installs < layerAfter ; 88return { ok :true , serves :true , split :false , choice :'mtp' , drafter :missing ?'off' :'mtp' , needed :false , gb :0 , mtp_needed :missing , mtp_gb :missing ?5.2 :0 , line :'Drafting: MTP' }; 89}; 90const installs = f => f . state . rpc . filter ( p => p . name === 'strata_install' && p . arguments ?. confirm === true ); 91const prepareAndLoad = f => f . native . operation ( 'install' , { family :'qwen' , model :'UD_Q4_K_XL' , gguf_dir :f . modelFolder , loadAfter :true }); 92 93test ( 'the install takes the drafter Control\'s Auto picks for this card: the MTP on a 12 GB streaming card, so the layer comes with the model' , async () => { 94const f = fixture ({ plan :planner ({ atInstall :{ choice :'mtp' , drafter :'mtp' } }) }); 95try { 96await f . native . refresh ( true ); 97const job = await finish ( f . native , prepareAndLoad ( f ). id ); 98assert . equal ( job . state , 'done' , job . error ); 99assert . equal ( installs ( f ). length , 1 , 'one install: the layer came with it, the load fetched nothing more' ); 100assert . equal ( installs ( f )[ 0 ]. arguments . drafter , 'mtp' , 'not the host\'s DFlash2 guess' ); 101const asked = f . state . planCalls . find ( c => ! c . configPath && c . family === 'qwen' ); 102assert . equal ( asked . mode , 'auto' ); 103assert . equal ( asked . model , 'UD_Q4_K_XL' ); 104} finally { await f . native . close (); f . cleanup (); } 105}); 106 107test ( 'where Control\'s Auto means DFlash2, the install skips the MTP layer as before' , async () => { 108const f = fixture ({ plan :planner ({ atInstall :{ choice :'dflash2' , drafter :'dflash2' }, layerAfter :0 }) }); 109try { 110await f . native . refresh ( true ); 111const job = await finish ( f . native , prepareAndLoad ( f ). id ); 112assert . equal ( job . state , 'done' , job . error ); 113assert . equal ( installs ( f )[ 0 ]. arguments . drafter , 'dflash2' ); 114} finally { await f . native . close (); f . cleanup (); } 115}); 116 117test ( 'the first load after a setup fetches a missing MTP layer (an older Control without the plan at install time), so it does not run undrafted' , async () => { 118const f = fixture ({ plan :planner ({ atInstall :new Error ( 'no plan' ), layerAfter :2 }) }); 119try { 120await f . native . refresh ( true ); 121const job = await finish ( f . native , prepareAndLoad ( f ). id ); 122assert . equal ( job . state , 'done' , job . error ); 123const all = installs ( f ); 124assert . equal ( all . length , 2 , 'the setup, then the layer' ); 125assert . equal ( all [ 0 ]. arguments . drafter , 'dflash2' , 'no plan: the old host rule' ); 126assert . equal ( all [ 1 ]. arguments . drafter , 'mtp' ); 127assert . equal ( all [ 1 ]. arguments . gguf_dir , dirname ( f . gguf )); 128} finally { await f . native . close (); f . cleanup (); } 129}); 130 131test ( 'a plain load that is not the first after a setup still downloads nothing' , async () => { 132const f = fixture ({ plan :planner ({ atInstall :{ choice :'mtp' }, layerAfter :5 }) }); 133try { 134await f . native . refresh ( true ); 135const job = await finish ( f . native , load ( f ). id ); 136assert . equal ( job . state , 'done' , job . error ); 137assert . equal ( installs ( f ). length , 0 ); 138} finally { await f . native . close (); f . cleanup (); } 139});