okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1b4018870
main
1/** 2* 0.2.14.x follow-ups of the app side (phaseA-tests notes-ctrl-fix-2.md): 3* (a) Drafting Off in the app reaches setup as `--mtp off` (the installer's `mtp` word), so no draft layer is fetched, on a Mac too (the Terminal's 1821f298d); 4* (b) the offline words (owner 11-15, the Terminal's drafting.MTP_OFFLINE_NOTE) are said where the app says its drafter notes (the job's drafterNote), and only when 5* no other drafter stands in: when Control's plan names a drafter that runs, the sentence "The model starts without a drafter." would be wrong. 6* The fixture is applin-0214-drafter.test.mjs's (fake Control, fake installer over MCP). 7*/ 8import './helpers/isolate-profile.mjs' ; // a test run never writes the owner's real Strata profile 9import test from 'node:test' ; 10import assert from 'node:assert/strict' ; 11import { EventEmitter } from 'node:events' ; 12import { mkdtempSync , mkdirSync , readFileSync , rmSync , writeFileSync } from 'node:fs' ; 13import { join } from 'node:path' ; 14import { tmpdir } from 'node:os' ; 15import { createNativeControl , MTP_OFFLINE_NOTE } from '../server/native-control.mjs' ; 16 17function fixture ({ plan}) { 18const root = mkdtempSync ( join ( tmpdir (), 'strata-fix0215-' )); 19const install = join ( root , 'install' ), data = join ( root , 'host-data' ), home = join ( root , 'home' ), modelsDir = join ( home , 'models' ); 20for ( const d of [ install , data , modelsDir ]) mkdirSync ( d , { recursive :true }); 21const earlier = process . env . STRATA_INSTALL_DIR ; 22process . env . STRATA_INSTALL_DIR = install ; 23const dataFolder = join ( root , 'Strata-data' ), modelFolder = join ( dataFolder , 'models' , 'UD-Q4_K_XL' ); 24const gguf = join ( modelFolder , 'Qwen3.8-Flash-Next-UD-Q4_K_XL-00001-of-00002.gguf' ); 25mkdirSync ( modelFolder , { recursive :true }); 26writeFileSync ( gguf , 'GGUF' ); writeFileSync ( gguf . replace ( '00001' , '00002' ), 'GGUF' ); 27const pack = join ( dataFolder , 'packs' , 'ud_q4_k_xl' ), mtp = join ( dataFolder , 'mtp' , 'rt' ); 28mkdirSync ( join ( pack , 'tokenizer' ), { recursive :true }); 29writeFileSync ( join ( pack , 'native_experts.txt' ), 'x' ); 30const configPath = join ( install , 'strata-ud_q4_k_xl.json' ); 31const args = [ '--pack' , pack , '--native' , gguf , '--ple-gguf' , gguf . replace ( '00001' , '00002' ), '--mtp' , mtp , '--max-context' , '131072' , '--kv' , 'int8' ]; 32writeFileSync ( configPath , JSON . stringify ({ exe :'component:strata-engine' , tokenizer :join ( pack , 'tokenizer' ), args, model_name :'qwen3.8-flash-next-ud_q4_k_xl' , port :8080 })); 33writeFileSync ( process . env . STRATA_SETTINGS_FILE , JSON . stringify ({ data_dir :join ( root , 'host-owned' , 'Strata-data' ), data_dir_at :1700000000 })); 34const state = { installs :0 , planCalls :[], rpc :[], starts :0 }; 35const control = { 36control :{ dir :join ( root , 'control' ), python :join ( root , 'control' , 'python' , 'python' ) }, version :'0.2.10' , 37async installedModels () { 38return { 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 :[] }] }; 39}, 40async engine () { return { usable :true , backend :'cuda' , components_dir :join ( root , 'c' ), engine :{ version :'0.1.60' , platform :'linux-x86_64' , backend :'cuda' , exe :join ( root , 'engine' , 'strata' ), root :join ( root , 'engine' ) } }; }, 41async draftingPlan ( options ) { state . planCalls . push ( options ); return plan ( options , state ); }, 42async draftingFetch () { return { ok :true , event :'result' , pack :null , skipped :false }; }, 43async splitEngine () { return { ok :true , split :false , line :'' , warn :false , reasons :[], written :null }; }, 44async saveProfile () { return {}; }, 45async start () { state . starts += 1 ; return { ok :true , state :'ready' }; }, 46async stop () { return { ok :true }; }, 47}; 48const catalog = { families :[{ family :'qwen' , title :'Qwen3.8-Flash-Next' , about :'x' , images :false , experimental :false , sizes :[ 49{ 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 }] }], 50contexts :[ 32768 , 131072 ], recommendation :{ family :'qwen' , model :'UD-Q4_K_XL' , context :131072 }, context_note :'' }; 51const 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 }] }; 52const spawnProcess = ( command , argv ) => { 53if ( ! String ( argv [ 0 ]). endsWith ( 'strata_mcp.py' )) throw new Error ( 'no process but the installer may be started in packaged mode: ' + command + ' ' + argv . join ( ' ' )); 54const child = new EventEmitter (); child . stdout = new EventEmitter (); child . stderr = new EventEmitter (); child . exitCode = null ; 55child . kill = () => { child . exitCode = 0 ; queueMicrotask (() => child . emit ( 'exit' , 0 )); return true ; }; 56child . stdin = { write ( line ) { 57const message = JSON . parse ( line ); if ( ! message . id ) return ; 58if ( message . method === 'tools/call' ) state . rpc . push ( message . params ); 59let result ; 60if ( message . method === 'initialize' ) result = { protocolVersion :'2024-11-05' }; 61else if ( message . method === 'tools/list' ) result = { tools :[ 'strata_models' , 'strata_status' , 'strata_install' , 'strata_remove' ]. map ( name => ({ name})) }; 62else if ( message . params . name === 'strata_models' ) result = { structuredContent :catalog }; 63else if ( message . params . name === 'strata_status' ) result = { structuredContent :{ hardware, recommendation :catalog . recommendation , install :null , running :[], summary :'x' } }; 64else if ( Object . keys ( message . params . arguments ?? {}). length === 0 ) result = { structuredContent :{ summary :'Install finished' , install :{ running :false , exit_code :0 } } }; 65else if ( message . params . arguments ?. confirm ) { state . installs += 1 ; result = { structuredContent :{ summary :'Install started' , plan :{}, install :{ running :true , step :'1/7' } } }; } // offline: the layer never arrives 66else result = { structuredContent :{ summary :'Plan' , plan :{ title :'Qwen3.8-Flash-Next UD-Q4_K_XL' , download_gb :5.2 , disk_short :null } } }; 67queueMicrotask (() => child . stdout . emit ( 'data' , Buffer . from ( JSON . stringify ({ jsonrpc :'2.0' , id :message . id , result}) + String . fromCharCode ( 10 )))); 68} }; 69return child ; 70}; 71const fetcher = async url => String ( url ). endsWith ( '/metrics' ) ?Response . json ({ engine :{} }) :Response . json ({ status :'ok' }); 72const native = createNativeControl ({ control, directory :data , spawnProcess, fetcher, requests :() => [], modelsDirectory :() => modelsDir , hardwareProbe :() => [{ index :0 , vram_gb :12 }] }); 73return { native, state, modelFolder, mtp, configPath, 74cleanup :() => { if ( earlier === undefined ) delete process . env . STRATA_INSTALL_DIR ; else process . env . STRATA_INSTALL_DIR = earlier ; rmSync ( root , { recursive :true , force :true }); } }; 75} 76const 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' ); }; 77const installs = f => f . state . rpc . filter ( p => p . name === 'strata_install' && p . arguments ?. confirm === true ); 78const atInstall = choice => ( options ) => ( ! options . configPath ?{ ok :true , serves :true , split :false , choice, drafter :choice , needed :false , mtp_needed :false , gb :0 } : 79{ ok :true , serves :true , split :false , choice, drafter :choice , needed :false , mtp_needed :false , gb :0 , line :'x' }); 80 81test ( '(a) Drafting Off at the install: setup is told --mtp off (no draft layer), and no drafter word' , async () => { 82const f = fixture ({ plan :atInstall ( 'off' ) }); 83try { 84await f . native . refresh ( true ); 85const job = await finish ( f . native , f . native . operation ( 'install' , { family :'qwen' , model :'UD_Q4_K_XL' , gguf_dir :f . modelFolder , drafting :'off' }). id ); 86assert . equal ( job . state , 'done' , job . error ); 87const call = installs ( f )[ 0 ]. arguments ; 88assert . equal ( call . mtp , 'off' ); 89assert . equal ( call . drafter , undefined ); 90} finally { await f . native . close (); f . cleanup (); } 91}); 92 93test ( '(a) Auto and the MTP keep the installer as before: no mtp word' , async () => { 94for ( const [ drafting , choice ] of [[ 'auto' , 'dflash2' ], [ 'mtp' , 'mtp' ], [ 'auto' , 'mtp' ]]) { 95const f = fixture ({ plan :atInstall ( choice ) }); 96try { 97await f . native . refresh ( true ); 98const job = await finish ( f . native , f . native . operation ( 'install' , { family :'qwen' , model :'UD_Q4_K_XL' , gguf_dir :f . modelFolder , drafting}). id ); 99assert . equal ( job . state , 'done' , job . error ); 100assert . equal ( installs ( f )[ 0 ]. arguments . mtp , undefined , ` ${ drafting } -> ${ choice } ` ); 101} finally { await f . native . close (); f . cleanup (); } 102} 103}); 104 105/** The Models screen's Load with the MTP chosen, offline: the layer step comes back without the layer. `standIn`: Control's plan after it names the drafter that runs. */ 106const offlineLoad = standIn => ( options , state ) => ( ! options . configPath ?{ ok :true , serves :true , choice :'dflash2' , split :false } : 107{ ok :true , serves :true , split :false , choice :'mtp' , drafter :state . installs === 0 ?'off' :standIn , needed :false , gb :0 , mtp_needed :true , mtp_gb :5.2 , line :'Drafting' }); 108 109test ( '(b) offline with no other drafter: the owner\'s sentence, word for word, in the job\'s drafter note' , async () => { 110const f = fixture ({ plan :offlineLoad ( 'off' ) }); 111try { 112await f . native . refresh ( true ); 113const job = await finish ( f . native , f . native . operation ( 'load' , { model :'ud_q4_k_xl' , values :{ drafting :'mtp' } }). id ); 114assert . equal ( job . state , 'done' , job . error ); 115assert . equal ( installs ( f ). length , 1 , 'the layer step ran once' ); 116assert . equal ( job . drafterNote , "There is no internet connection, so the draft layer can't be downloaded now. The model starts without a drafter." ); 117assert . equal ( MTP_OFFLINE_NOTE , job . drafterNote ); 118} finally { await f . native . close (); f . cleanup (); } 119}); 120 121test ( '(b) offline but the DFlash2 drafter on this computer stands in: the sentence is not said' , async () => { 122const f = fixture ({ plan :offlineLoad ( 'dflash2' ) }); 123try { 124await f . native . refresh ( true ); 125const job = await finish ( f . native , f . native . operation ( 'load' , { model :'ud_q4_k_xl' , values :{ drafting :'mtp' } }). id ); 126assert . equal ( job . state , 'done' , job . error ); 127assert . equal ( installs ( f ). length , 1 ); 128assert . notEqual ( job . drafterNote , MTP_OFFLINE_NOTE ); 129} finally { await f . native . close (); f . cleanup (); } 130}); 131 132/** Item 7 (owner 11-15): a drafter chosen in the app's Drafting setting stays chosen at later loads with no new choice, and the fallback a start takes (Control's plan 133* names the other drafter: the chosen one's file is not here) is never written over it. */ 134test ( 'a chosen MTP stays the saved choice over a later plain load, even when the start falls back to DFlash2' , async () => { 135const seen = []; 136const plan = ( options ) => { 137if ( ! options . configPath ) return { ok :true , serves :true , choice :'dflash2' , split :false }; 138seen . push ( JSON . parse ( readFileSync ( options . configPath , 'utf8' )). drafting ?? null ); 139return { ok :true , serves :true , split :false , choice :'mtp' , drafter :'dflash2' , needed :false , gb :0 , mtp_needed :false , mtp_gb :0 , line :'Drafting: DFlash2 (the MTP drafter isn\'t downloaded yet)' }; 140}; 141const f = fixture ({ plan}); 142try { 143await f . native . refresh ( true ); 144let job = await finish ( f . native , f . native . operation ( 'load' , { model :'ud_q4_k_xl' , values :{ drafting :'mtp' } }). id ); 145assert . equal ( job . state , 'done' , job . error ); 146assert . equal ( JSON . parse ( readFileSync ( f . configPath , 'utf8' )). drafting , 'mtp' , 'the choice is saved as the person set it, not the fallback' ); 147job = await finish ( f . native , f . native . operation ( 'load' , { model :'ud_q4_k_xl' }). id ); 148assert . equal ( job . state , 'done' , job . error ); 149assert . equal ( JSON . parse ( readFileSync ( f . configPath , 'utf8' )). drafting , 'mtp' , 'a plain load keeps it' ); 150assert . equal ( seen . at ( - 1 ), 'mtp' , 'and asks Control about the chosen MTP, not Auto' ); 151assert . equal ( installs ( f ). length , 0 , 'nothing was fetched for it' ); 152} finally { await f . native . close (); f . cleanup (); } 153}); 154 155test ( '(b) the Terminal and the app hold the same words' , () => { 156const py = readFileSync ( new URL ( '../../../strata_control/drafting.py' , import . meta. url ), 'utf8' ); 157assert . ok ( py . includes ( `MTP_OFFLINE_NOTE = " ${ MTP_OFFLINE_NOTE } "` )); 158});