okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1#!/usr/bin/env node 2/** 3* "Use your own GGUF" end to end on a box, through the installed app layout (packaged host + bundled Control + a real engine component): 4* preview a folder of a supported model, preview an unsupported GGUF (must be refused in plain terms), prepare the supported one 5* (pack, from the folder, nothing copied), load it, get a chat answer, unload, shut down, and check nothing is left running. 6* 7* node local-gguf-e2e.mjs --root /data/ui4-e2e --good /data/141-gyro/models/IQ2_XS --bad /data/ui4-e2e/bad [--context 8192] [--backend cuda|metal] 8* (the app folder is <root>/app: node/, host/, control/ ...; <root>/components gets the engine, from <root>/app/engine-bundle when the app carries one) 9*/ 10import { spawn , spawnSync } from 'node:child_process' ; 11import { existsSync , mkdirSync , readFileSync , writeFileSync } from 'node:fs' ; 12import { createServer } from 'node:net' ; 13import { join , resolve } from 'node:path' ; 14 15const args = Object . fromEntries ( process . argv . slice ( 2 ). reduce (( acc , a , i , all ) => ( a . startsWith ( '--' ) ?[ ...acc , [ a . slice ( 2 ), all [ i + 1 ]]] :acc ), [])); 16const root = resolve ( args . root ); 17const good = args . good , bad = args . bad ; 18const context = Number ( args . context ?? 8192 ); 19const app = join ( root , 'app' ); 20const dirs = { components :join ( root , 'components' ), install :join ( root , 'install' ), profiles :join ( root , 'profiles' ), runtime :join ( root , 'runtime' ), home :join ( root , 'home' ), xdg :join ( root , 'xdg' ), appdata :join ( root , 'appdata' ) }; 21for ( const d of [ dirs . install , dirs . home , dirs . xdg , dirs . appdata ]) mkdirSync ( d , { recursive :true }); 22const delay = ms => new Promise ( r => setTimeout ( r , ms )); 23const steps = []; 24let failed = false ; 25const record = ( name , ok , detail = '' ) => { steps . push ({ name, ok :!! ok , detail :String ( detail ). slice ( 0 , 400 ) }); console . log ( ` ${ ok ? 'ok ' : 'FAIL' } ${ name } ${ detail ? ' ' + String ( detail ). slice ( 0 , 300 ) : '' } ` ); if ( ! ok ) failed = true ; }; 26const freePort = async () => { const s = createServer (); await new Promise ( r => s . listen ( 0 , '127.0.0.1' , r )); const p = s . address (). port ; await new Promise ( r => s . close ( r )); return p ; }; 27 28const env = { ...process . env , STRATA_COMPONENTS_DIR :dirs . components , STRATA_PROFILE_DIR :dirs . profiles , STRATA_RUNTIME_DIR :dirs . runtime , STRATA_INSTALL_DIR :dirs . install , STRATA_HOME :dirs . home , STRATA_MODELS_DIR :join ( root , 'Strata-data' ), STRATA_ENGINE_BACKEND :args . backend ?? 'cuda' , XDG_CONFIG_HOME :dirs . xdg , APPDATA :dirs . appdata , PYTHONDONTWRITEBYTECODE :'1' }; 29delete env . STRATA_API_KEY ; 30const port = await freePort (); 31const base = `http://127.0.0.1: ${ port } ` ; 32const host = spawn ( join ( app , 'node' , 'node' ), [ join ( app , 'host' , 'server' , 'index.mjs' )], { cwd :join ( app , 'host' ), stdio :[ 'ignore' , 'pipe' , 'pipe' ], detached :false , 33env :{ ...env , STRATA_PORT :String ( port ), STRATA_DIST :join ( app , 'host' , 'dist' ), NODE_ENV :'production' , STRATA_PACKAGED :'1' , STRATA_REAL_ENGINE :'1' , STRATA_NO_ACQUIRE :'1' , STRATA_EXIT_WITH_PARENT :'1' , 34STRATA_SHARE_PORT :String ( await freePort ()), STRATA_TUNNEL_PORT :String ( await freePort ()), STRATA_WORKSPACE :join ( root , 'workspace' ) } }); 35mkdirSync ( join ( root , 'workspace' ), { recursive :true }); 36let hostLog = '' ; host . stdout . on ( 'data' , c => { hostLog += c ; }); host . stderr . on ( 'data' , c => { hostLog += c ; }); 37const call = async ( path , method = 'GET' , body ) => { const r = await fetch ( base + path , { method, headers :{ 'content-type' :'application/json' }, body :body === undefined ?undefined :JSON . stringify ( body ) }); return { status :r . status , body :await r . json (). catch (() => null ) }; }; 38async function waitJob ( id , seconds ) { 39const until = Date . now () + seconds * 1000 ; 40let last = '' ; 41while ( Date . now () < until ) { 42const state = ( await call ( '/api/native' )). body ; 43const job = state . jobs . find ( j => j . id === id ); 44if ( job && job . state !== 'running' ) return { job, state}; 45if ( job && job . summary !== last ) { last = job . summary ; console . log ( ' ...' , String ( job . summary ). slice ( 0 , 140 )); } 46await delay ( 2000 ); 47} 48throw new Error ( 'operation did not finish in time' ); 49} 50const engineProcs = () => spawnSync ( 'ps' , [ '-eo' , 'pid,args' ], { encoding :'utf8' }). stdout . split ( '\n' ). filter ( l => l . includes ( join ( dirs . components , 'engine' )) && / \/strata ( | $ ) / . test ( l )); 51 52async function main () { 53let up = false ; 54for ( let i = 0 ; i < 200 && ! up ; i ++ ) { try { up = ( await fetch ( base + '/api/health' )). ok ; } catch { await delay ( 150 ); } } 55record ( 'host healthy' , up , up ?base :hostLog . slice ( - 500 )); 56if ( ! up ) return ; 57let status = ( await call ( '/api/control/status' )). body ; 58for ( let i = 0 ; i < 180 && status ?. engineSetup ; i ++ ) { await delay ( 1000 ); status = ( await call ( '/api/control/status' )). body ; } // the engine that came with the app is copied on the first start 59record ( 'Control status: bundled, engine usable' , status ?. engine ?. usable === true , `engine= ${ status ?. engine ?. engine ?. version } ${ status ?. engine ?. notice ?? '' } ` ); 60await call ( '/api/native?refresh=1' ); 61 62// ---- the unsupported GGUF: a plain refusal 63const badPreview = await call ( '/api/native/local/preview' , 'POST' , { path :bad }); 64const badIssues = badPreview . body ?. preview ?. issues ?? badPreview . body ?. issues ?? []; 65const badStatus = badPreview . body ?. preview ?. file_set_status ; 66console . log ( ' bad preview:' , badPreview . status , JSON . stringify ( badPreview . body ). slice ( 0 , 400 )); 67record ( 'an unsupported GGUF is refused, nothing adopted' , badPreview . status === 200 ?( badStatus !== 'plausible' || ! badPreview . body . preview . candidate_selection ) :badPreview . status >= 400 , `status= ${ badPreview . status } ; file_set_status= ${ badStatus } ; issues= ${ JSON . stringify ( badIssues )} ` ); 68 69// ---- the supported folder 70const goodPreview = await call ( '/api/native/local/preview' , 'POST' , { path :good }); 71const selection = goodPreview . body ?. preview ?. candidate_selection ; 72console . log ( ' good preview:' , JSON . stringify ( goodPreview . body ). slice ( 0 , 500 )); 73record ( 'a folder of a supported model is recognised' , goodPreview . status === 200 && goodPreview . body ?. preview ?. file_set_status === 'plausible' && !! selection , `family= ${ selection ?. family } ; model= ${ selection ?. model } ; files= ${ goodPreview . body ?. preview ?. file_count } ` ); 74if ( ! selection || args . only === 'preview' ) return ; 75 76// ---- prepare (what the page's button does): install from the folder, nothing copied or downloaded 77const native0 = ( await call ( '/api/native?refresh=1' )). body ; 78const catalogSize = native0 . catalog ?. families ?. find ( f => f . family === selection . family )?. sizes . find ( s => s . model === selection . model ); 79record ( 'the catalog lists the recognised version' , !! catalogSize , catalogSize ?`id= ${ catalogSize . id } ` :`no ${ selection . family } / ${ selection . model } in the catalog` ); 80const started = await call ( '/api/native/operations' , 'POST' , { kind :'install' , args :{ family :selection . family , model :selection . model , gguf_dir :good , context, vision :'no' } }); 81record ( 'prepare starts' , started . status < 300 && !! started . body ?. id , `status= ${ started . status } ${ JSON . stringify ( started . body ). slice ( 0 , 200 )} ` ); 82if ( ! started . body ?. id ) return ; 83const prepared = await waitJob ( started . body . id , 3600 ); 84record ( 'prepare finishes' , prepared . job . state === 'done' , ` ${ prepared . job . state } : ${ prepared . job . error ?? prepared . job . summary } ` ); 85if ( prepared . job . state !== 'done' ) return ; 86const native1 = ( await call ( '/api/native?refresh=1' )). body ; 87const modelId = catalogSize ?. id ?? ` ${ selection . family === 'qwen' ? '' : selection . family + '-' } ${ String ( selection . model ). toLowerCase ()} ` ; 88const entry = native1 . installed ?. models ?. find ( m => m . model === modelId ); 89record ( 'the model is ready and points at the folder, not a copy' , entry ?. ready === true , `model= ${ modelId } ; ready= ${ entry ?. ready } ; dataFolder= ${ entry ?. dataFolder } ` ); 90if ( ! entry ?. ready ) return ; 91 92// ---- load 93const load = await call ( '/api/native/operations' , 'POST' , { kind :'load' , args :{ model :modelId } }); 94const loaded = await waitJob ( load . body . id , 1800 ); 95const instance = loaded . state . instances [ 0 ]; 96record ( 'load starts the engine from the component folder' , loaded . job . state === 'done' && instance ?. status === 'ready' , `job= ${ loaded . job . state } ${ loaded . job . error ?? '' } ; instance= ${ instance ?. status } ` ); 97if ( instance ?. status !== 'ready' ) return ; 98record ( 'one engine process is running' , engineProcs (). length === 1 , engineProcs (). map ( l => l . trim (). slice ( 0 , 140 )). join ( ' | ' )); 99 100// ---- a chat answer, checked 101const chat = await fetch ( base + '/api/chat/stream' , { method :'POST' , headers :{ 'content-type' :'application/json' }, body :JSON . stringify ({ sessionId :'e2e-chat' , message :'Reply with exactly one word: pong' , history :[], nativeModelId :instance . id , thinking :'Off' , agent :false , workspaceId :null }) }); 102let text = '' , done = null ; 103for await ( const chunk of chat . body ) { 104for ( const line of Buffer . from ( chunk ). toString ( 'utf8' ). split ( '\n' )) { 105if ( ! line . trim ()) continue ; 106try { const f = JSON . parse ( line ); if ( f . type === 'delta' ) text += f . text ?? '' ; if ( f . type === 'done' ) done = f ; if ( f . type === 'error' ) text += ` [error: ${ f . message } ]` ; } catch { /* partial line */ } 107} 108} 109record ( 'the model answers through the app (the answer text is checked)' , chat . status === 200 && / pong / i . test ( text ), `status= ${ chat . status } ; answer= ${ JSON . stringify ( text . slice ( 0 , 120 ))} ; tokensOut= ${ done ?. tokensOut } ; tps= ${ done ?. tps } ` ); 110 111// ---- unload, shutdown, nothing left 112const unload = await call ( `/api/native/instances/ ${ instance . id } /unload` , 'POST' ); 113await delay ( 3000 ); 114record ( 'unload stops the engine' , unload . status < 300 && engineProcs (). length === 0 , `status= ${ unload . status } ; engines left= ${ engineProcs (). length } ` ); 115} 116 117try { await main (); } catch ( e ) { record ( 'the flow' , false , e ?. stack ?? e ); } 118host . kill (); 119for ( let i = 0 ; i < 100 && host . exitCode === null ; i ++ ) await delay ( 100 ); 120if ( host . exitCode === null ) host . kill ( 'SIGKILL' ); 121await delay ( 2000 ); 122const left = spawnSync ( 'ps' , [ '-eo' , 'pid,args' ], { encoding :'utf8' }). stdout . split ( '\n' ). filter ( l => ( l . includes ( dirs . install ) || l . includes ( join ( app , 'host' ))) && ! l . includes ( 'ps -eo' )); 123record ( 'nothing of this run is left running' , left . length === 0 , left . join ( ' | ' ). slice ( 0 , 300 )); 124writeFileSync ( join ( root , 'e2e-result.json' ), JSON . stringify ({ steps, hostLogTail :hostLog . slice ( - 2000 ) }, null , 1 )); 125console . log ( failed ?'FAILED' :'ALL OK' ); 126process . exit ( failed ?1 :0 );