okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2a3e33b8e
main
1import './helpers/isolate-profile.mjs' ; // a test run never writes the owner's real Strata profile 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { EventEmitter } from 'node:events' ; 5import { existsSync } from 'node:fs' ; 6import { mkdtempSync , mkdirSync , writeFileSync , rmSync , readFileSync , realpathSync } from 'node:fs' ; 7import { join , basename } from 'node:path' ; 8import { tmpdir as osTmpdir } from 'node:os' ; 9// the canonical scratch root: on a Mac the temp folder is /var/folders/..., which resolves to /private/var/...; the paths the host reports are the resolved ones 10const tmpdir = () => realpathSync ( osTmpdir ()); 11import { bundledPython , componentsDirectory , controlEnvironment , createControl , findControl , runCli } from '../server/control-runtime.mjs' ; 12import { resolvePython } from '../server/python-resolver.mjs' ; 13import { createNativeControl } from '../server/native-control.mjs' ; 14 15const isWin = process . platform === 'win32' ; 16 17function fakeControlDir () { 18const dir = mkdtempSync ( join ( tmpdir (), 'strata-control-dir-' )); 19mkdirSync ( join ( dir , 'python' , ...( isWin ?[] :[ 'bin' ])), { recursive :true }); 20mkdirSync ( join ( dir , 'strata_control' ), { recursive :true }); 21mkdirSync ( join ( dir , 'serve' ), { recursive :true }); 22writeFileSync ( bundledPython ( dir ), 'x' ); 23writeFileSync ( join ( dir , 'strata.py' ), '# cli' ); 24writeFileSync ( join ( dir , 'serve' , 'server.py' ), 'port\nparallel\n/v1/responses' ); 25writeFileSync ( join ( dir , 'BUNDLE.json' ), JSON . stringify ({ version :'0.1.45' , python :'3.12.15' })); 26return dir ; 27} 28 29test ( 'the Control component is found by STRATA_CONTROL_DIR or beside the host, and only if it is whole' , () => { 30const dir = fakeControlDir (); 31// The explicit env has no STRATA_COMPONENTS_DIR, so findControl would look in the real per-user components folder and find an updated Control installed on this PC. 32const none = join ( dir , 'no-components' ); 33try { 34const found = findControl ({ env :{ STRATA_CONTROL_DIR :dir }, hostDir :join ( dir , 'nowhere' ), components :none }); 35assert . equal ( found . dir , dir ); 36assert . equal ( found . python , bundledPython ( dir )); 37assert . equal ( found . bundle . python , '3.12.15' ); 38// beside the host: <install>/host + <install>/control 39const install = mkdtempSync ( join ( tmpdir (), 'strata-install-' )); 40try { 41mkdirSync ( join ( install , 'host' , 'server' ), { recursive :true }); 42assert . equal ( findControl ({ env :{}, hostDir :join ( install , 'host' ), components :none }), null , 'nothing beside the host yet' ); 43mkdirSync ( join ( install , 'control' , 'python' , ...( isWin ?[] :[ 'bin' ])), { recursive :true }); 44mkdirSync ( join ( install , 'control' , 'strata_control' )); 45writeFileSync ( bundledPython ( join ( install , 'control' )), 'x' ); 46writeFileSync ( join ( install , 'control' , 'strata.py' ), '#' ); 47assert . equal ( findControl ({ env :{}, hostDir :join ( install , 'host' ), components :none }). dir , join ( install , 'control' )); 48} finally { rmSync ( install , { recursive :true , force :true }); } 49rmSync ( join ( dir , 'strata.py' )); 50assert . equal ( findControl ({ env :{ STRATA_CONTROL_DIR :dir }, hostDir :join ( dir , 'x' ), components :none }), null , 'a half component is not used' ); 51} finally { rmSync ( dir , { recursive :true , force :true }); } 52}); 53 54test ( "Control runs on its own interpreter with nothing from the user's Python and acquiring forbidden" , () => { 55const control = { dir :'/c' , python :'/c/python/python3' , cli :'/c/strata.py' }; 56const env = controlEnvironment ( control , { env :{ PYTHONHOME :'x' , PYTHONPATH :'y' , VIRTUAL_ENV :'z' , STRATA_PYTHON :'q' , PATH :'p' , STRATA_API_KEY :'secret' }, components :'/components' }); 57assert . equal ( env . PYTHONHOME , undefined ); 58assert . equal ( env . PYTHONPATH , undefined ); 59assert . equal ( env . VIRTUAL_ENV , undefined ); 60assert . equal ( env . STRATA_PYTHON , undefined ); 61assert . equal ( env . PYTHONNOUSERSITE , '1' ); 62assert . equal ( env . STRATA_NO_ACQUIRE , '1' ); 63assert . equal ( env . STRATA_COMPONENTS_DIR , '/components' ); 64assert . equal ( env . STRATA_API_KEY , undefined , 'an inherited inference key never reaches the engine (the run config carries its own)' ); 65assert . equal ( env . PATH , 'p' ); 66// the resolver prefers the bundled interpreter over py -3 / PATH / a venv 67const python = resolvePython ({ repo :'/repo' , control, env :{}, exists :() => true , starts :() => { throw new Error ( 'must not probe' ); }, useCache :false }); 68assert . deepEqual ([ python . command , python . source ], [ '/c/python/python3' , 'bundled' ]); 69assert . equal ( resolvePython ({ control, env :{ STRATA_PYTHON :'/mine' }, useCache :false }). command , '/mine' , 'an explicit choice still wins' ); 70assert . equal ( componentsDirectory ({ env :{ STRATA_COMPONENTS_DIR :'/x' } }), '/x' ); 71assert . match ( componentsDirectory ({ env :{ LOCALAPPDATA :'C:\\L' }, platform :'win32' , home :'C:\\H' }), / Strata [ \\ / ] components$ / ); 72}); 73 74test ( 'runCli passes the arguments untouched (no shell) and reads JSON' , async () => { 75const seen = []; 76const spawnProcess = ( command , args , options ) => { 77seen . push ({ command, args, options}); 78const child = new EventEmitter (); child . stdout = new EventEmitter (); child . stderr = new EventEmitter (); child . kill = () => {}; 79queueMicrotask (() => { child . stdout . emit ( 'data' , '{"ok": true, "n": 1}\n' ); child . emit ( 'close' , 0 ); }); 80return child ; 81}; 82const control = { dir :'/c' , python :'/c/python/python3' , cli :'/c/strata.py' }; 83const result = await runCli ( control , [ 'models' , 'list' , '--installed' , '--output' , 'json' ], { spawnProcess, env :{} }); 84assert . deepEqual ( result . json , { ok :true , n :1 }); 85assert . deepEqual ( seen [ 0 ]. args , [ '/c/strata.py' , 'models' , 'list' , '--installed' , '--output' , 'json' ]); 86assert . equal ( seen [ 0 ]. options . shell , undefined ); 87assert . equal ( seen [ 0 ]. options . windowsHide , true ); 88assert . equal ( seen [ 0 ]. options . env . STRATA_NO_ACQUIRE , '1' ); 89}); 90 91// ---------------------------------------------------------------------------------- native-control in packaged mode 92 93function packagedFixture ({ engineUsable= true , updater= null , repair= false , ids= [ 'iq2_xs' , 'iq3_s' ], controlVersion= '0.1.45' , extraSizes= [], cliStartWait= 'old' , installPoll= null } = {}) { 94const root = mkdtempSync ( join ( tmpdir (), 'strata-packaged-test-' )); 95const install = join ( root , 'install' ), data = join ( root , 'data' ); 96mkdirSync ( install , { recursive :true }); 97/* The Control this app found, as it is on disk. The app reads the start-wait ceiling out of these files instead of trusting 98the version, because a start wait is checked three times before the engine is reached and a released Control has those 99three ceilings disagreeing: 100'old' no runtime ceiling at all, both gates 600 (Control before #3949) 101'split' the runtime takes 1800, the CLI and the record still stop at 600 (every Control 0.1.50 to 0.1.52) 102'cli-only' the CLI follows the runtime, the record still stops at 600 (`direct runtime intent has an invalid timeout`) 103'runtime' all three hand the wait to the runtime's own ceiling */ 104const controlSource = join ( root , 'control' , 'strata_control' ); 105mkdirSync ( controlSource , { recursive :true }); 106writeFileSync ( join ( controlSource , 'runtime.py' ), cliStartWait === 'old' ?'' :'MAX_START_WAIT = 1800\n' ); 107writeFileSync ( join ( controlSource , 'management.py' ), [ 'cli-only' , 'runtime' ]. includes ( cliStartWait ) 108 ?' wait = _seconds(wait, 0, runtime.MAX_START_WAIT, "start wait")\n' 109 :' wait = _seconds(wait, 0, 600, "start wait")\n' ); 110writeFileSync ( join ( controlSource , 'journal.py' ), cliStartWait === 'runtime' 111 ?' ranges = {"wait_s": (0, start_wait_max) if kind == "runtime.start" else\n' 112 :' ranges = {"wait_s": (0, 600) if kind == "runtime.start" else\n' ); 113const earlierInstallEnv = process . env . STRATA_INSTALL_DIR ; 114process . env . STRATA_INSTALL_DIR = install ; // the app's own install folder (the host always tells Control which one; it never falls back to an older CLI folder) 115const configs = ids . map ( id => { 116const path = join ( install , `strata- ${ id } .json` ); 117writeFileSync ( path , JSON . stringify ({ exe :'component:strata-engine' , tokenizer :join ( install , 'tok' ), args :[ '--max-context' , '32768' ], model_name :`Qwen ${ id } ` , port :8080 })); 118return { id, path}; 119}); 120const calls = []; 121const saved = []; 122const spawned = []; 123const rpc = []; 124let startFailure = null , startFailureLeft = Infinity ; 125let statusAnswer = { state :'ready' }, metricsDown = false , healthDown = false ; 126const control = { 127control :{ dir :join ( root , 'control' ), python :join ( root , 'control' , 'python' , 'python.exe' ) }, version :controlVersion , 128async installedModels ( options ) { calls . push ([ 'installedModels' , options ?. installDir ?? null ]); return { install_dir :install , models :configs . map ( c => ({ id :c . id , name :`Qwen ${ c . id } ` , source_config :c . path , component_status :'present' , context_tokens :32768 , backend :'cuda' , ...( / ^deepseek / . test ( c . id ) ?{ component_status :'retired' , retired :'deepseek' , name :'DeepSeek-V4.1-Flash ISTA-DAS 2.00 bpw' , retired_line :'DeepSeek - no longer supported.' , refusal :'DeepSeek-V4.1-Flash ISTA-DAS 2.00 bpw is a DeepSeek model. This version of Strata no longer runs DeepSeek models, so it was not started. Choose a Qwen model to run instead. If you no longer need the DeepSeek files, you can delete that folder to free the space.' } :{}) })) }; }, 129async engine () { 130calls . push ([ 'engine' ]); 131return engineUsable 132 ?{ usable :true , backend :'cuda' , components_dir :'E:\\c' , engine :{ version :'0.1.45' , platform :'windows-x86_64' , backend :'cuda' , exe :'E:\\engine\\strata.exe' , root :'E:\\engine' } } 133 :{ usable :false , notice :'No engine for this PC was found in E:\\c: the component folder holds no engine package. The app never downloads an engine while it starts or serves.' , backend :'cuda' , engine :null , components_dir :'E:\\c' }; 134}, 135async importPlan ( path ) { calls . push ([ 'importPlan' , path ]); return { file_set_status :'plausible' , candidate_selection :{ family :'qwen' , model :'IQ3_S' }, file_count :2 , total_bytes :1e9 , issues :[] }; }, 136async saveProfile ( name , path ) { calls . push ([ 'saveProfile' , name , basename ( path )]); saved . push ( JSON . parse ( readFileSync ( path , 'utf8' ))); return {}; }, 137async start ( name , options ) { calls . push ([ 'start' , name , options ?. waitSeconds ]); if ( startFailure && startFailureLeft -- > 0 ) { const e = new Error ( startFailure . message ); e . output = startFailure . output ; throw e ; } return { ok :true , state :'ready' }; }, 138async stop ( name ) { calls . push ([ 'stop' , name ]); return { ok :true , state :'stopped' }; }, 139async status ( name ) { calls . push ([ 'status' , name ]); return typeof statusAnswer === 'function' ?statusAnswer ( name ) :statusAnswer ; }, 140 ...( repair ?{ async repairAccess () { calls . push ([ 'repairAccess' ]); return { ok :true }; } } :{}), 141 ...( updater ?{ async updateCheck () { calls . push ([ 'updateCheck' ]); return updater . report ; }, async update ( options ) { calls . push ([ 'update' , options . engine , options . control ]); return updater . update ( options ); } } :{}), 142}; 143const requests = []; 144const fetcher = async url => { 145calls . push ([ 'fetch' , url . replace ( / :\d +/ , ':PORT' )]); 146if ( metricsDown && / \/ ( metrics | mcp ) / . test ( url )) throw new Error ( 'the engine does not answer' ); 147if ( healthDown && / \/health / . test ( url )) throw new Error ( 'the engine does not answer' ); 148return Response . json ({ status :'ok' }); 149}; 150// The only process the host may start is Control's installer (its bundled tools/strata_mcp.py on Control's Python); the fake 151// answers like it: the catalog, the hardware with the GPUs, and an install plan or job. 152const catalog = { families :[{ family :'qwen' , title :'Qwen3.8-Flash-Next' , about :'x' , images :true , experimental :false , sizes :[ 153{ id :'q2_0' , model :'Q2_0' , download_gb :66.4 , ram_needed_gb :48 , about :'fast' , on_this_pc :'fits' , installed :false , images :true , experimental :false }, 154{ id :'iq3_xxs' , model :'IQ3_XXS' , download_gb :75.8 , ram_needed_gb :60 , about :'balanced' , on_this_pc :'fits' , installed :false , images :true , experimental :false }, ...extraSizes ] }], 155contexts :[ 8192 , 32768 ], recommendation :{ family :'qwen' , model :'IQ3_XXS' , context :32768 }, context_note :'' }; 156const hardware = { ram_gb :63 , cpu :{ name :'cpu' , cores :12 }, gpus :[{ index :0 , name :'NVIDIA GeForce RTX 5070' , vram_gb :11.9 , usable :true }] }; 157let installJob = null ; 158const spawnProcess = ( command , args , options ) => { 159spawned . push ({ command, args, options}); 160if ( ! String ( args [ 0 ]). endsWith ( 'strata_mcp.py' )) throw new Error ( 'no process but the installer may be started by the host in packaged mode: ' + command + ' ' + args . join ( ' ' )); 161const child = new EventEmitter (); child . stdout = new EventEmitter (); child . stderr = new EventEmitter (); child . exitCode = null ; 162child . kill = () => { child . exitCode = 0 ; queueMicrotask (() => child . emit ( 'exit' , 0 )); return true ; }; 163child . stdin = { write ( line ) { 164const message = JSON . parse ( line ); if ( ! message . id ) return ; 165if ( message . method === 'tools/call' ) rpc . push ( message . params ); 166let result ; 167if ( message . method === 'initialize' ) result = { protocolVersion :'2024-11-05' }; 168else if ( message . method === 'tools/list' ) result = { tools :[ 'strata_models' , 'strata_status' , 'strata_install' , 'strata_remove' ]. map ( name => ({ name})) }; 169else if ( message . params . name === 'strata_models' ) result = { structuredContent :catalog }; 170else if ( message . params . name === 'strata_remove' ) result = { structuredContent :{ summary :'Deleted' , plan :{ gb :1.2 }, failed :[] } }; 171else if ( message . params . name === 'strata_status' ) result = { structuredContent :{ hardware, recommendation :catalog . recommendation , install :installJob , running :[], summary :'x' } }; 172else if ( message . params . arguments ?. confirm ) { installJob = { running :false , exit_code :0 }; result = { structuredContent :{ summary :'Install started' , plan :{ download_gb :66.4 }, install :{ running :true , step :'downloading' } } }; } 173else if ( message . params . arguments ?. cancel === undefined && message . params . arguments ?. confirm === undefined ) result = { structuredContent :installPoll ?? { summary :'Install finished' , install :{ running :false , exit_code :0 } } }; 174else result = { structuredContent :{ summary :'Plan' , plan :{ title :'Qwen3.8-Flash-Next Q2_0' , download_gb :66.4 , disk_short :null } } }; 175queueMicrotask (() => child . stdout . emit ( 'data' , Buffer . from ( JSON . stringify ({ jsonrpc :'2.0' , id :message . id , result}) + String . fromCharCode ( 10 )))); 176} }; 177return child ; 178}; 179const native = createNativeControl ({ control, directory :data , spawnProcess, fetcher, requests :() => requests }); 180const others = () => spawned . filter ( s => ! String ( s . args [ 0 ]). endsWith ( 'strata_mcp.py' )); 181return { native, calls, saved, spawned, others, rpc, setStatus :v => { statusAnswer = v ; }, setMetricsDown :v => { metricsDown = v ; }, setHealthDown :v => { healthDown = v ; }, failStart :( v , times = Infinity ) => { startFailure = v ; startFailureLeft = times ; }, catalog, hardware, install, root, cleanup :() => { if ( earlierInstallEnv === undefined ) delete process . env . STRATA_INSTALL_DIR ; else process . env . STRATA_INSTALL_DIR = earlierInstallEnv ; rmSync ( root , { recursive :true , force :true }); } }; 182} 183const finish = async ( native , id ) => { for ( let i = 0 ; i < 400 ; 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' ); }; 184 185test ( 'packaged: models come from Control, no python, git or engine process is started by the host' , async () => { 186const f = packagedFixture (); 187try { 188const state = await f . native . refresh ( true ); 189assert . equal ( state . available , true , state . error ); 190assert . deepEqual ( state . installed . models . map ( m => m . model ), [ 'iq2_xs' , 'iq3_s' ]); 191assert . equal ( state . installed . models [ 0 ]. ready , true ); 192assert . equal ( state . packaged , true ); 193assert . match ( state . summary , / engine 0\.1\.45 / ); 194assert . equal ( state . version , '0.1.45' ); 195assert . equal ( f . others (). length , 0 , 'nothing but the installer is started' ); 196// the catalog, the GPUs and the recommendation come from the bundled installer, run on Control's own Python 197assert . equal ( state . catalog . families [ 0 ]. sizes . length , 2 ); 198assert . equal ( state . catalog . recommendation . model , 'IQ3_XXS' ); 199assert . equal ( state . hardware . gpus [ 0 ]. name , 'NVIDIA GeForce RTX 5070' ); 200assert . equal ( state . hardware . ram_gb , 63 ); 201const installer = f . spawned [ 0 ]; 202assert . equal ( installer . command , join ( f . root , 'control' , 'python' , 'python.exe' )); 203assert . deepEqual ( installer . args . slice ( 1 ), [ '--root' , f . install , '--src' , join ( f . root , 'control' )]); 204assert . equal ( installer . options . env . STRATA_CONTROL_INSTALL , '1' ); 205assert . equal ( installer . options . env . STRATA_NO_ACQUIRE , '1' , 'the installer process itself may not acquire; only its explicit install job does' ); 206assert . equal ( installer . options . env . STRATA_INSTALL_DIR , f . install ); 207assert . equal ( installer . options . env . PYTHONNOUSERSITE , '1' ); 208} finally { await f . native . close (); f . cleanup (); } 209}); 210 211test ( 'packaged: load = saved profile + runtime start under Control; unload = runtime stop; two models get two profiles' , async () => { 212const f = packagedFixture (); 213try { 214await f . native . refresh ( true ); 215const load = async args => { const job = await finish ( f . native , f . native . operation ( 'load' , args ). id ); assert . equal ( job . state , 'done' , job . error ); return job ; }; 216await load ({ model :'iq2_xs' }); 217const first = f . native . state (). instances [ 0 ]; 218assert . equal ( first . status , 'ready' ); 219const startCalls = f . calls . filter ( c => [ 'saveProfile' , 'start' ]. includes ( c [ 0 ])); 220assert . deepEqual ( startCalls . map ( c => c . slice ( 0 , 2 )), [[ 'saveProfile' , 'app-1' ], [ 'start' , 'app-1' ]]); 221assert . equal ( startCalls [ 1 ][ 2 ], 300 ); 222await load ({ model :'iq3_s' , multiple :true }); 223assert . deepEqual ( f . calls . filter ( c => c [ 0 ] === 'start' ). map ( c => c [ 1 ]), [ 'app-1' , 'app-2' ]); 224assert . equal ( f . native . state (). instances . length , 2 ); 225await f . native . unload ( first . id ); 226assert . deepEqual ( f . calls . filter ( c => c [ 0 ] === 'stop' ). at ( - 1 ), [ 'stop' , 'app-1' ]); 227assert . equal ( f . native . state (). instances . length , 1 ); 228assert . equal ( f . others (). length , 0 , 'the host never spawned anything itself' ); 229} finally { await f . native . close (); f . cleanup (); } 230}); 231 232test ( 'packaged: a DeepSeek model of an earlier Strata (an id with dots) is listed with its one line; its load is refused with Control\'s words and starts nothing' , async () => { 233const id = 'deepseek-v4.1-flash-ist-das-2.00bpw' ; 234const f = packagedFixture ({ ids :[ id ] }); 235try { 236const path = join ( f . install , `strata- ${ id } .json` ); 237writeFileSync ( path , JSON . stringify ({ exe :join ( f . install , 'engine-dsv41' , 'strata-dsv41' ), arch :'deepseek41' , tokenizer :join ( f . install , 'tok' ), args :[ '--pack' , join ( f . install , 'pack' ), '--max-context' , '8192' , '--eos' , '1' ], model_name :'deepseek-v4.1-flash' , port :8080 , engine_ready_s :3600 })); 238const before = readFileSync ( path , 'utf8' ); 239const state = await f . native . refresh ( true ); 240assert . deepEqual ( state . installed . models . map ( m => m . model ), [ id ], 'listed' ); 241assert . equal ( state . installed . models [ 0 ]. ready , false ); 242assert . equal ( state . installed . models [ 0 ]. retired . line , 'DeepSeek - no longer supported.' ); 243const job = await finish ( f . native , f . native . operation ( 'load' , { model :id }). id ); 244assert . equal ( job . state , 'error' ); 245assert . match ( String ( job . error ), / ^DeepSeek-V4\.1-Flash ISTA-DAS 2\.00 bpw is a DeepSeek model\. This version of Strata no longer runs DeepSeek models, so it was not started\. / ); 246assert . deepEqual ( f . calls . filter ( c => [ 'saveProfile' , 'start' ]. includes ( c [ 0 ])), [], 'nothing is saved or started' ); 247assert . equal ( readFileSync ( path , 'utf8' ), before , 'its settings file is not written' ); 248for ( const odd of [ 'a..b' , '../x' , 'x/y' , '.hidden' ]) assert . equal (( await finish ( f . native , f . native . operation ( 'load' , { model :odd }). id )). state , 'error' , odd ); 249} finally { await f . native . close (); f . cleanup (); } 250}); 251 252test ( 'packaged: no engine for this PC is the notice, and nothing is started' , async () => { 253const f = packagedFixture ({ engineUsable :false }); 254try { 255await f . native . refresh ( true ); 256const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 257assert . equal ( job . state , 'error' ); 258assert . match ( job . error , / No engine for this PC / ); 259assert . match ( job . error , / never downloads an engine / ); 260assert . equal ( f . calls . some ( c => [ 'saveProfile' , 'start' ]. includes ( c [ 0 ])), false ); 261assert . equal ( f . others (). length , 0 ); 262} finally { await f . native . close (); f . cleanup (); } 263}); 264 265test ( 'packaged: the user can download and prepare a model: the plan and the install job go to the bundled installer, the engine is not touched' , async () => { 266const f = packagedFixture (); 267try { 268await f . native . refresh ( true ); 269const plan = await f . native . plan ({ family :'qwen' , model :'Q2_0' }); 270assert . equal ( plan . plan . title , 'Qwen3.8-Flash-Next Q2_0' ); 271const planned = f . rpc . at ( - 1 ); 272assert . equal ( planned . name , 'strata_install' ); 273assert . equal ( planned . arguments . confirm , false , 'a plan changes nothing' ); 274const job = await finish ( f . native , f . native . operation ( 'install' , { family :'qwen' , model :'Q2_0' }). id ); 275assert . equal ( job . state , 'done' , job . error ); 276const started = f . rpc . filter ( c => c . name === 'strata_install' && c . arguments . confirm ); 277assert . equal ( started . length , 1 ); 278assert . equal ( started [ 0 ]. arguments . family , 'qwen' ); 279assert . equal ( started [ 0 ]. arguments . model , 'Q2_0' ); 280assert . equal ( f . others (). length , 0 , 'the host starts no pip, git, python of its own or engine' ); 281assert . equal ( f . calls . filter ( c => [ 'saveProfile' , 'start' ]. includes ( c [ 0 ])). length , 0 , 'installing does not start an engine' ); 282} finally { await f . native . close (); f . cleanup (); } 283}); 284 285test ( 'packaged: the installer works in a folder the app can write, never in the read-only bundle' , async () => { 286const f = packagedFixture (); 287try { 288await f . native . refresh ( true ); 289assert . equal ( existsSync ( f . install ), true ); 290assert . notEqual ( f . native . state (). installDir , join ( f . root , 'control' )); 291assert . equal ( f . spawned [ 0 ]. args . at ( - 3 ), f . install ); 292} finally { await f . native . close (); f . cleanup (); } 293}); 294 295test ( 'packaged: update, rollback and calibration are refused with the Terminal notice and start nothing' , async () => { 296const f = packagedFixture (); 297try { 298await f . native . refresh ( true ); 299for ( const [ kind , args ] of [[ 'update-engine' , {}], [ 'update-strata' , {}], [ 'rollback' , {}], [ 'calibrate' , { model :'iq3_s' }]]) { 300const job = await finish ( f . native , f . native . operation ( kind , args ). id ); 301assert . equal ( job . state , 'error' , kind ); 302assert . match ( job . error , / Terminal | calibrates NVIDIA | No saved previous engine / , ` ${ kind } : ${ job . error } ` ); 303} 304assert . equal ( f . others (). length , 0 ); 305assert . equal ( f . calls . filter ( c => [ 'saveProfile' , 'start' ]. includes ( c [ 0 ])). length , 0 ); 306} finally { await f . native . close (); f . cleanup (); } 307}); 308 309test ( 'packaged: the install folder can be chosen, and it is what Control is asked about' , async () => { 310const f = packagedFixture (); 311try { 312const other = join ( f . root , 'other' ); 313mkdirSync ( other ); 314await f . native . configure ({ repository :other }); 315const saved = JSON . parse ( readFileSync ( join ( f . root , 'data' , 'native-host.json' ), 'utf8' )); 316assert . equal ( saved . installDir . toLowerCase (), other . toLowerCase ()); 317assert . equal ( f . calls . filter ( c => c [ 0 ] === 'installedModels' ). at ( - 1 )[ 1 ]. toLowerCase (), other . toLowerCase ()); 318} finally { await f . native . close (); f . cleanup (); } 319}); 320 321test ( 'createControl asks the CLI for the engine, models and runtime and reads the notice of a missing engine' , async () => { 322const seen = []; 323const control = createControl ({ 324control :{ dir :'/c' , python :'/p' , cli :'/c/strata.py' , bundle :{ version :'9' } }, 325run :async args => { seen . push ( args . join ( ' ' )); return args [ 0 ] === 'components' ?{ code :5 , json :{ ok :false , usable :false , notice :'No engine for this PC' }, text :'' , stderr :'' } :{ code :0 , json :{ ok :true , profiles :[] }, text :'' , stderr :'' }; }, 326}); 327assert . equal (( await control . engine ()). notice , 'No engine for this PC' ); 328await control . installedModels ({ installDir :'D:\\Strata' }); 329await control . saveProfile ( 'app-1' , 'C:\\x.json' ); 330await control . start ( 'app-1' ); 331await control . stop ( 'app-1' ); 332assert . deepEqual ( seen , [ 333'components engine --output json' , 334'models list --installed --install-dir D:\\Strata --output json' , 335'profile list --output json' , 336'profile create app-1 --from-config C:\\x.json --output json' , 337`runtime start --profile app-1 --wait 300 --shutdown-timeout 15 --die-with-pid ${ process . pid } --output json` , 338'runtime stop --profile app-1 --wait 120 --output json' , 339]); 340assert . equal ( createControl ({ control :null }), null ); 341}); 342 343test ( 'packaged: the catalog offers no picture reading, an install asks for none by default and for it only when Read pictures was ticked' , async () => { 344const f = packagedFixture (); 345try { 346const state = await f . native . refresh ( true ); 347assert . ok ( state . catalog . families [ 0 ]. sizes . every ( s => s . images === false )); 348assert . ok ( state . catalog . families . every ( fam => fam . images === false )); 349await f . native . plan ({ family :'qwen' , model :'Q2_0' }); 350assert . equal ( f . rpc . at ( - 1 ). arguments . vision , 'no' ); 351await f . native . plan ({ family :'qwen' , model :'Q2_0' , vision :'yes' }); 352assert . equal ( f . rpc . at ( - 1 ). arguments . vision , 'yes' ); 353} finally { await f . native . close (); f . cleanup (); } 354}); 355 356test ( 'packaged: an engine that stops while loading keeps the code and the last engine line for the Details disclosure' , async () => { 357const f = packagedFixture (); 358try { 359await f . native . refresh ( true ); 360f . failStart ({ message :'server_exited' , output :{ log_tail :'memory guard: 5.1 GiB free is under the 6 GiB limit' } }); 361const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 362assert . equal ( job . state , 'error' ); 363assert . match ( job . error , / ^server_exited: memory guard: 5\.1 GiB free / ); 364} finally { await f . native . close (); f . cleanup (); } 365}); 366 367test ( 'packaged: a start that died because Windows ran out of locked memory is tried once more with a smaller pin, said in plain words, and the saved settings stay as they were' , async () => { 368const f = packagedFixture (); 369try { 370await f . native . refresh ( true ); 371f . failStart ({ message :'server_exited' , output :{ log_tail :'verify: mapped staging allocation failed [pinned-memory-exhausted] the OS gave no more page-locked memory; start again with STRATA_ARENA_PIN_GIB=26 (pin less of the expert arena)' } }, 1 ); 372const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 373assert . equal ( job . state , 'done' , job . error ); 374assert . equal ( f . calls . filter ( c => c [ 0 ] === 'start' ). length , 2 , 'one try and exactly one more' ); 375assert . match ( job . result . summary , / ready, after a second try: Windows would not lock as much memory / ); 376assert . match ( job . details [ 0 ], / pinned-memory-exhausted / ); 377assert . match ( job . details [ 1 ], / STRATA_ARENA_PIN_GIB=26 / ); 378const instance = f . native . state (). instances [ 0 ]; 379assert . equal ( instance . status , 'ready' ); 380const saved = JSON . parse ( readFileSync ( join ( f . install , 'strata-iq2_xs.json' ), 'utf8' )); 381assert . equal ( saved . env , undefined , 'the safer setting is for this start only; the model config is not changed' ); 382} finally { await f . native . close (); f . cleanup (); } 383}); 384 385test ( 'packaged: the second try is the last one: a second failure is reported with both, and nothing loops' , async () => { 386const f = packagedFixture (); 387try { 388await f . native . refresh ( true ); 389f . failStart ({ message :'server_exited' , output :{ log_tail :'mapped staging allocation failed [pinned-memory-exhausted]' } }); 390const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 391assert . equal ( job . state , 'error' ); 392assert . equal ( f . calls . filter ( c => c [ 0 ] === 'start' ). length , 2 ); 393assert . doesNotMatch ( job . error , / first stop was / , 'the same stop twice is said once, not repeated' ); 394assert . match ( job . details . join ( ' ' ), / Second try / , 'the second try stays in the details' ); 395} finally { await f . native . close (); f . cleanup (); } 396}); 397 398test ( 'packaged: retrySafer false keeps the single try' , async () => { 399const f = packagedFixture (); 400try { 401await f . native . refresh ( true ); 402f . failStart ({ message :'server_exited' , output :{ log_tail :'mapped staging allocation failed [pinned-memory-exhausted]' } }); 403const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' , retrySafer :false }). id ); 404assert . equal ( job . state , 'error' ); 405assert . equal ( f . calls . filter ( c => c [ 0 ] === 'start' ). length , 1 ); 406} finally { await f . native . close (); f . cleanup (); } 407}); 408 409test ( 'packaged: a Control whose wait ran out has not stopped the engine, so the load is kept and watched, not failed (#5177 #4293)' , async () => { 410/* `runtime start --wait N` answers "startup_wait_expired" (exit 8) while the engine is still loading, and "runtime operation wait expired" when the owner 411was too busy to answer a status question in time; neither stops the engine. Control is asked: starting or ready means the app keeps the load. */ 412for ( const message of [ 'The engine did not start.' , 'runtime operation wait expired; inspect runtime status' ]) { 413const f = packagedFixture (); 414try { 415await f . native . refresh ( true ); 416f . setStatus ({ ok :true , state :'starting' , phase :'loading' }); 417f . failStart ({ message, output :{ ok :false , state :'starting' , error :message . startsWith ( 'runtime' ) ?undefined :'startup_wait_expired' , phase :'loading' , exit_code :8 } }, 1 ); 418const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 419assert . equal ( job . state , 'done' , job . error ); 420assert . equal ( f . calls . filter ( c => c [ 0 ] === 'start' ). length , 1 , 'a wait that ran out is not a failure: no second try' ); 421assert . equal ( f . calls . filter ( c => c [ 0 ] === 'stop' ). length , 1 , 'the one stop is startWith clearing the slot; the loading engine is never stopped by the app' ); 422assert . equal ( f . native . state (). instances [ 0 ]. status , 'ready' , 'ready once /health answers' ); 423assert . ok ( f . calls . some ( c => c [ 0 ] === 'status' && c [ 1 ] === 'app-1' ), 'Control was asked before the load was kept' ); 424} finally { await f . native . close (); f . cleanup (); } 425} 426}); 427 428test ( 'packaged: an expired wait with an engine Control says is gone is a failure, and an adopted load whose engine dies ends as one' , async () => { 429const f = packagedFixture (); 430try { 431await f . native . refresh ( true ); 432f . setStatus ({ ok :false , state :'failed' , error :'server_exited' , engine_exit_code :1 }); 433f . failStart ({ message :'The engine did not start.' , output :{ ok :false , state :'starting' , error :'startup_wait_expired' , exit_code :8 } }, 1 ); 434const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' , retrySafer :false }). id ); 435assert . equal ( job . state , 'error' , 'Control says the engine is gone: nothing to adopt' ); 436} finally { await f . native . close (); f . cleanup (); } 437const g = packagedFixture (); 438try { 439await g . native . refresh ( true ); 440g . setHealthDown ( true ); // never answers /health ... 441let asked = 0 ; // the first look (is the wait's engine still loading?) says starting, every later one says it ended 442g . setStatus (() => ( ++ asked === 1 ?{ ok :true , state :'starting' , phase :'loading' } :{ ok :false , state :'failed' , error :'server_exited' , engine_exit_code :3 })); 443g . failStart ({ message :'The engine did not start.' , output :{ ok :false , state :'starting' , error :'startup_wait_expired' , exit_code :8 } }, 1 ); 444const started = g . native . operation ( 'load' , { model :'iq2_xs' }); 445const job = await finish ( g . native , started . id ); 446assert . equal ( job . state , 'error' ); 447assert . match ( String ( job . error ?? '' ), / server_exited / ); 448} finally { await g . native . close (); g . cleanup (); } 449}); 450 451test ( 'packaged: a ready instance whose engine stopped answering is marked stopped once Control says so, and stays ready while the engine is only busy (#4973)' , async () => { 452const f = packagedFixture (); 453try { 454await f . native . refresh ( true ); 455const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 456assert . equal ( job . state , 'done' , job . error ); 457f . setMetricsDown ( true ); 458f . setStatus ({ ok :true , state :'ready' }); // busy, not gone 459await new Promise ( r => setTimeout ( r , 5200 )); 460assert . equal ( f . native . state (). instances [ 0 ]. status , 'ready' ); 461assert . equal ( f . native . state (). instances [ 0 ]. phase , 'unavailable' ); 462f . setStatus ({ ok :true , state :'stopped' }); // the owner left with its engine 463await new Promise ( r => setTimeout ( r , 2600 )); 464const instance = f . native . state (). instances [ 0 ]; 465assert . equal ( instance . status , 'error' ); 466assert . match ( instance . error , / Engine stopped / ); 467} finally { await f . native . close (); f . cleanup (); } 468}); 469 470test ( 'packaged: a run config that points into an old Control folder is repaired to this Control (a reinstall or an update moved it)' , async () => { 471const f = packagedFixture (); 472try { 473const here = join ( f . root , 'control' ); 474mkdirSync ( join ( here , 'data' ), { recursive :true }); 475writeFileSync ( join ( here , 'data' , 'expert-profile.bin' ), 'profile' ); 476const path = join ( f . install , 'strata-iq2_xs.json' ); 477const old = 'C:/Users/Alex/AppData/Local/Temp/strata-life-install/control' ; 478writeFileSync ( path , JSON . stringify ({ exe :'component:strata-engine' , cwd :old , args :[ '--pack' , 'D:/Strata/packs/x' , '--expert-profile' , old + '/data/expert-profile.bin' , '--max-context' , '32768' ], model_name :'x' , port :8080 })); 479await f . native . refresh ( true ); 480const repaired = JSON . parse ( readFileSync ( path , 'utf8' )); 481assert . equal ( repaired . cwd . toLowerCase (), here . toLowerCase ()); 482assert . equal ( repaired . args [ repaired . args . indexOf ( '--expert-profile' ) + 1 ]. toLowerCase (), join ( here , 'data' , 'expert-profile.bin' ). toLowerCase ()); 483assert . equal ( repaired . args [ 1 ], 'D:/Strata/packs/x' , 'paths outside Control are left alone' ); 484assert . ok ( existsSync ( path + '.bak' ), 'the old config is kept' ); 485// a config that is already right, or whose new target is not there, is not touched 486const before = readFileSync ( path , 'utf8' ); 487await f . native . refresh ( true ); 488assert . equal ( readFileSync ( path , 'utf8' ), before ); 489} finally { await f . native . close (); f . cleanup (); } 490}); 491 492test ( 'packaged: deleting a downloaded model unloads it first, then the installer removes its files' , async () => { 493const f = packagedFixture (); 494try { 495await f . native . refresh ( true ); 496const load = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 497assert . equal ( load . state , 'done' , load . error ); 498assert . equal ( f . native . state (). instances . length , 1 ); 499const job = await finish ( f . native , f . native . operation ( 'remove' , { model :'iq2_xs' }). id ); 500assert . equal ( job . state , 'done' , job . error ); 501assert . equal ( f . native . state (). instances . length , 0 , 'the loaded model was unloaded first' ); 502assert . deepEqual ( f . calls . filter ( c => c [ 0 ] === 'stop' ). at ( - 1 ), [ 'stop' , 'app-1' ]); 503const removed = f . rpc . filter ( c => c . name === 'strata_remove' ); 504assert . equal ( removed . length , 1 ); 505assert . deepEqual ( removed [ 0 ]. arguments , { model :'iq2_xs' , confirm :true }); 506assert . equal ( f . others (). length , 0 ); 507const bad = await finish ( f . native , f . native . operation ( 'remove' , { model :'../x' }). id ); 508assert . equal ( bad . state , 'error' , 'an id that is not an installed model is refused' ); 509} finally { await f . native . close (); f . cleanup (); } 510}); 511 512test ( 'packaged: a local GGUF folder is checked by Control, then installed from where it is (no download)' , async () => { 513const f = packagedFixture (); 514try { 515await f . native . refresh ( true ); 516const preview = await f . native . localPreview ( 'D:/models/iq3s' ); 517assert . equal ( preview . candidate_selection . model , 'IQ3_S' ); 518assert . deepEqual ( f . calls . find ( c => c [ 0 ] === 'importPlan' ), [ 'importPlan' , 'D:/models/iq3s' ]); 519await assert . rejects (() => f . native . localPreview ( ' ' ), / Choose a folder / ); 520await f . native . plan ({ family :'qwen' , model :'IQ3_S' , gguf_dir :'D:/models/iq3s' }); 521assert . equal ( f . rpc . at ( - 1 ). arguments . gguf_dir , 'D:/models/iq3s' ); 522} finally { await f . native . close (); f . cleanup (); } 523}); 524 525test ( 'packaged: the install job also fetches the engine through Control (merge of the model progress and the engine step); a failed engine keeps the model and says so' , async () => { 526const report = { engine :{ status :'install' , offered :{ version :'0.1.46' }, download_bytes :5e8 } }; 527const f = packagedFixture ({ engineUsable :false , updater :{ report, async update ( options ) { 528options . onEvent ({ event :'progress' , phase :'download' , percent :50 , done :2.5e8 , total :5e8 }); 529return { ok :true , results :[{ component :'engine' , status :'updated' , installed_after :{ version :'0.1.46' } }] }; 530} } }); 531try { 532await f . native . refresh ( true ); 533const job = await finish ( f . native , f . native . operation ( 'install' , { family :'qwen' , model :'Q2_0' }). id ); 534assert . equal ( job . state , 'done' , job . error ); 535assert . equal ( job . result . install . engine . state , 'done' ); 536assert . deepEqual ( job . result . install . parts . map ( p => p . kind ), [ 'model' , 'engine' ]); 537assert . deepEqual ( f . calls . filter ( c => c [ 0 ] === 'update' ), [[ 'update' , true , false ]], 'the engine only, from this install job' ); 538assert . equal ( f . calls . filter ( c => [ 'saveProfile' , 'start' ]. includes ( c [ 0 ])). length , 0 ); 539} finally { await f . native . close (); f . cleanup (); } 540const g = packagedFixture ({ engineUsable :false , updater :{ report, async update () { throw new Error ( 'the download of http://x/e.tar.gz was interrupted after 100 of 200 bytes (ConnectionResetError)' ); } } }); 541try { 542await g . native . refresh ( true ); 543const job = await finish ( g . native , g . native . operation ( 'install' , { family :'qwen' , model :'Q2_0' }). id ); 544assert . equal ( job . state , 'done' , job . error ); 545assert . match ( job . summary , / Your model is ready\. The engine for this PC couldn't be downloaded / ); 546assert . equal ( job . result . install . engine . state , 'failed' ); 547assert . doesNotMatch ( job . summary , / http | ConnectionReset | bytes / ); 548} finally { await g . native . close (); g . cleanup (); } 549}); 550 551test ( 'packaged: a model install that fails does not cancel the engine download it started (DL99: a retry would fail the same way again)' , async () => { 552const report = { engine :{ status :'install' , offered :{ version :'0.1.46' }, download_bytes :5e8 } }; 553const seen = { finished :false , aborted :null }; 554const f = packagedFixture ({ engineUsable :false , 555installPoll :{ summary :'failed' , install :{ running :false , exit_code :1 , error :'this engine (99.0.0) does not run Qwen3.6-35B-A3B yet - update Strata' } }, 556updater :{ report, async update ( options ) { 557options . onEvent ({ event :'progress' , phase :'download' , percent :40 , done :2e8 , total :5e8 }); 558await new Promise ( r => setTimeout ( r , 2500 )); // longer than the install job's first poll (1.5 s), which reads the failure 559seen . aborted = options . signal ?. aborted === true ; seen . finished = true ; 560return { ok :true , results :[{ component :'engine' , status :'updated' , installed_after :{ version :'0.1.46' } }] }; 561} } }); 562try { 563await f . native . refresh ( true ); 564const job = await finish ( f . native , f . native . operation ( 'install' , { family :'qwen' , model :'Q2_0' }). id ); 565assert . equal ( job . state , 'error' ); 566assert . match ( job . error , / does not run Qwen3\.6-35B-A3B yet / ); 567assert . equal ( seen . finished , true , 'the engine download ran to its end before the job ended' ); 568assert . equal ( seen . aborted , false , 'the engine download was not cancelled' ); 569assert . equal ( job . result . install . engine . state , 'done' ); 570} finally { await f . native . close (); f . cleanup (); } 571}); 572 573test ( 'packaged: with no engine on the PC yet, the engine is installed before setup starts (setup takes the picture reader from it; Qwen3.6 came up text-only)' , async () => { 574const report = { engine :{ status :'install' , offered :{ version :'0.1.60' }, download_bytes :7e8 } }; 575const seen = { installsWhenEngineDone :null }; 576let f ; 577f = packagedFixture ({ engineUsable :false , updater :{ report, async update ( options ) { 578options . onEvent ({ event :'progress' , phase :'download' , percent :50 , done :3.5e8 , total :7e8 }); 579await new Promise ( r => setTimeout ( r , 300 )); 580seen . installsWhenEngineDone = f . rpc . filter ( c => c . name === 'strata_install' && c . arguments . confirm ). length ; 581return { ok :true , results :[{ component :'engine' , status :'updated' , installed_after :{ version :'0.1.60' } }] }; 582} } }); 583try { 584await f . native . refresh ( true ); 585const job = await finish ( f . native , f . native . operation ( 'install' , { family :'qwen' , model :'Q2_0' }). id ); 586assert . equal ( job . state , 'done' , job . error ); 587assert . equal ( seen . installsWhenEngineDone , 0 , 'setup was not started before the engine was in' ); 588assert . equal ( f . rpc . filter ( c => c . name === 'strata_install' && c . arguments . confirm ). length , 1 ); 589assert . deepEqual ( f . calls . filter ( c => c [ 0 ] === 'update' ), [[ 'update' , true , false ]], 'the engine is fetched once' ); 590assert . equal ( job . result . install . engine . state , 'done' ); 591} finally { await f . native . close (); f . cleanup (); } 592// an engine that is there already: setup starts at once and the engine step runs beside it, as before 593const g = packagedFixture ({ engineUsable :true , updater :{ report :{ engine :{ status :'current' , installed :{ version :'0.1.60' } } }, async update () { throw new Error ( 'not called' ); } } }); 594try { 595await g . native . refresh ( true ); 596const job = await finish ( g . native , g . native . operation ( 'install' , { family :'qwen' , model :'Q2_0' }). id ); 597assert . equal ( job . state , 'done' , job . error ); 598assert . equal ( g . calls . filter ( c => c [ 0 ] === 'update' ). length , 0 , 'a current engine is not fetched' ); 599} finally { await g . native . close (); g . cleanup (); } 600}); 601 602test ( 'packaged: Stop still cancels the engine download of an install' , async () => { 603const report = { engine :{ status :'install' , offered :{ version :'0.1.46' }, download_bytes :5e8 } }; 604const seen = { aborted :null }; 605const f = packagedFixture ({ engineUsable :false , 606installPoll :{ summary :'running' , install :{ running :true , step :'5/7: downloading' } }, 607updater :{ report, async update ( options ) { 608await new Promise ( resolve => { if ( options . signal ?. aborted ) resolve (); else options . signal ?. addEventListener ( 'abort' , resolve , { once :true }); }); 609seen . aborted = true ; 610throw new Error ( 'stopped' ); 611} } }); 612try { 613await f . native . refresh ( true ); 614const id = f . native . operation ( 'install' , { family :'qwen' , model :'Q2_0' }). id ; 615await new Promise ( r => setTimeout ( r , 300 )); 616await f . native . cancel ( id ); 617const job = await finish ( f . native , id ); 618assert . equal ( job . state , 'cancelled' ); 619for ( let i = 0 ; i < 100 && seen . aborted === null ; i ++ ) await new Promise ( r => setTimeout ( r , 10 )); 620assert . equal ( seen . aborted , true ); 621await new Promise ( r => setTimeout ( r , 2000 )); // the install loop's 1.5 s wait ends and it returns before the folder goes 622} finally { await f . native . close (); f . cleanup (); } 623}); 624 625test ( 'packaged: the app default of several requests at once and the memory retry share ONE second try, never two' , async () => { 626const f = packagedFixture (); f . hardware . gpus [ 0 ]. vram_gb = 48 ; 627try { 628await f . native . refresh ( true ); 629f . failStart ({ message :'server_exited' , output :{ log_tail :'something else went wrong' } }, 1 ); 630const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 631assert . equal ( job . state , 'done' , job . error ); 632assert . equal ( f . calls . filter ( c => c [ 0 ] === 'start' ). length , 2 , 'the failed first start and one second try' ); 633assert . match ( job . result . summary , / after a second try / ); 634assert . equal ( f . native . state (). instances [ 0 ]. parallel , 1 , 'the second try runs one request at a time' ); 635} finally { await f . native . close (); f . cleanup (); } 636const g = packagedFixture (); g . hardware . gpus [ 0 ]. vram_gb = 48 ; 637try { 638await g . native . refresh ( true ); 639g . failStart ({ message :'server_exited' , output :{ log_tail :'mapped staging allocation failed [pinned-memory-exhausted]' } }); 640const job = await finish ( g . native , g . native . operation ( 'load' , { model :'iq2_xs' }). id ); 641assert . equal ( job . state , 'error' ); 642assert . equal ( g . calls . filter ( c => c [ 0 ] === 'start' ). length , 2 , 'memory change and parallel drop ride on the same single try' ); 643} finally { await g . native . close (); g . cleanup (); } 644}); 645 646test ( 'packaged: a tune measures the engine a load starts: same start args, slots included, for the same model and profile' , async () => { 647for ( const vram of [ 11.9 , 48 ]) { 648const f = packagedFixture (); f . hardware . gpus [ 0 ]. vram_gb = vram ; 649try { 650await f . native . refresh ( true ); 651const args = { model :'iq2_xs' , resourceProfile :'work' , vision :'no' }; 652await f . native . tuneStatus ( args ). catch (() => {}); // the fake host cannot run Control's tuner; the config it would measure is written first 653const tuned = JSON . parse ( readFileSync ( join ( f . root , 'data' , 'native-tune' , 'iq2_xs.json' ), 'utf8' )); 654const job = await finish ( f . native , f . native . operation ( 'load' , args ). id ); 655assert . equal ( job . state , 'done' , job . error ); 656const loaded = { ...f . saved . at ( - 1 ) }; delete loaded . port ; delete loaded . api_key ; 657assert . deepEqual ( tuned . args , loaded . args , 'the engine arguments are identical' ); 658for ( const [ key , value ] of Object . entries ( tuned )) assert . deepEqual ( loaded [ key ], value , ` ${ key } is the same in the tune and the load` ); // a load adds only its own identity (aliases, model name, key, port) 659if ( vram > 40 ) assert . ok ( tuned . parallel > 1 , 'a big card gets the app default of several slots in the tune too' ); 660} finally { await f . native . close (); f . cleanup (); } 661} 662}); 663 664test ( 'packaged: a profile an elevated run locked (PermissionError) is repaired once and the start is tried again; without repair it is a plain message, not a memory retry' , async () => { 665const f = packagedFixture ({ repair :true }); 666try { 667await f . native . refresh ( true ); 668f . failStart ({ message :'cannot read profile (PermissionError)' }, 1 ); 669const job = await finish ( f . native , f . native . operation ( 'load' , { model :'iq2_xs' }). id ); 670assert . equal ( job . state , 'done' , job . error ); 671assert . equal ( f . calls . filter ( c => c [ 0 ] === 'repairAccess' ). length , 1 ); 672assert . equal ( f . calls . filter ( c => c [ 0 ] === 'start' ). length , 2 ); 673} finally { await f . native . close (); f . cleanup (); } 674const g = packagedFixture ({ repair :true }); 675try { 676await g . native . refresh ( true ); 677g . failStart ({ message :'cannot read profile (PermissionError)' }); 678const job = await finish ( g . native , g . native . operation ( 'load' , { model :'iq2_xs' }). id ); 679assert . equal ( job . state , 'error' ); 680assert . equal ( g . calls . filter ( c => c [ 0 ] === 'start' ). length , 2 , 'one repair try, no memory retry on top' ); 681assert . match ( job . error , / PermissionError / ); 682} finally { await g . native . close (); g . cleanup (); } 683}); 684 685test ( 'a GGUF folder Control refuses (exit 5, ok:false) still comes back as a checked plan with its reasons, so the page can say it in words' , async () => { 686const plan = { file_set_status :'invalid_or_incomplete' , candidate_selection :null , issues :[ 'x.gguf: GGUF tensor or metadata count exceeds the inspection limit' , 'unrecognized_or_unsupported_model_variant' ] }; 687const control = createControl ({ control :{ dir :'/c' , python :'/p' , cli :'/c/strata.py' , bundle :{ version :'9' } }, run :async () => ({ code :5 , json :{ ok :false , plan}, text :'' , stderr :'' }) }); 688assert . deepEqual ( await control . importPlan ( '/some/folder' ), plan ); 689const broken = createControl ({ control :{ dir :'/c' , python :'/p' , cli :'/c/strata.py' , bundle :{ version :'9' } }, run :async () => ({ code :1 , json :null , text :'' , stderr :'boom' }) }); 690await assert . rejects (() => broken . importPlan ( '/some/folder' ), / boom / ); 691}); 692 693test ( "preparing an adopted GGUF folder again never takes the folder's parent for the data folder (it is the person's, not Strata's)" , async () => { 694const f = packagedFixture (); 695try { 696// The model is already adopted: its run config points at a GGUF in someone's own ".../models/IQ3_S" folder. 697const own = join ( f . root , 'own' ); 698const configPath = join ( f . install , 'strata-iq3_s.json' ); 699const config = JSON . parse ( readFileSync ( configPath , 'utf8' )); 700writeFileSync ( configPath , JSON . stringify ({ ...config , args :[ ...config . args , '--native' , join ( own , 'models' , 'IQ3_S' , 'a-00001-of-00002.gguf' )] })); 701f . catalog . families [ 0 ]. sizes . push ({ id :'iq3_s' , model :'IQ3_S' , download_gb :83 , ram_needed_gb :62 , about :'x' , on_this_pc :'fits' , installed :true , images :true , experimental :false }); 702await f . native . refresh ( true ); 703await f . native . plan ({ family :'qwen' , model :'IQ3_S' , gguf_dir :join ( own , 'models' , 'IQ3_S' ) }); 704const sent = f . rpc . at ( - 1 ). arguments ; 705assert . notEqual ( sent . data_dir , own , "the person's own folder is not used for the packs" ); 706assert . equal ( sent . gguf_dir , join ( own , 'models' , 'IQ3_S' )); 707} finally { await f . native . close (); f . cleanup (); } 708}); 709 710test ( 'the app never adopts an older command-line install: Control is always told the app\'s own folder, and has its own settings file' , async () => { 711const f = packagedFixture (); 712try { 713await f . native . refresh ( true ); 714const asked = f . calls . filter ( c => c [ 0 ] === 'installedModels' ). map ( c => c [ 1 ]); 715assert . ok ( asked . length > 0 && asked . every ( dir => dir === f . install || ( dir && dir . toLowerCase () === f . install . toLowerCase ())), `Control was asked about ${ JSON . stringify ( asked )} ` ); 716} finally { f . cleanup (); } 717const env = controlEnvironment ({ dir :'/c' }, { env :{ APPDATA :'/old/appdata' }, components :join ( tmpdir (), 'Strata' , 'components' ) }); 718assert . equal ( env . STRATA_SETTINGS_FILE , join ( tmpdir (), 'Strata' , 'app-settings.json' )); 719}); 720 721test ( "onboarding 'use your own GGUF' (#3507, #3424): the models folder the person picked holds the GGUF files, so preparing them never takes it for the data folder" , async () => { 722const f = packagedFixture (); 723try { 724const own = join ( f . root , 'Strata-data' , 'models' , 'unsloth-UD-Q4_K_XL' ); 725mkdirSync ( own , { recursive :true }); 726await f . native . refresh ( true ); 727f . native . setDownloadDirectory ( own ); // what the first-run folder chooser wrote 728await f . native . plan ({ family :'qwen' , model :'IQ3_S' , gguf_dir :own }); 729const sent = f . rpc . at ( - 1 ). arguments ; 730assert . equal ( sent . gguf_dir , own ); 731assert . notEqual ( sent . data_dir , own , 'the folder of GGUF files is not the data folder: the packs would be downloaded into it' ); 732// a folder of GGUF files named like a data folder's models/<tag> hands its Strata folder to the installer 733assert . ok ( sent . data_dir === undefined || sent . data_dir === join ( f . root , 'Strata-data' ), `data_dir was ${ sent . data_dir } ` ); 734} finally { await f . native . close (); f . cleanup (); } 735}); 736 737/* The start wait follows the model's size, as `strata launch` does since #3949, and never asks an older Control for more than it takes. 738Reported on the app side by #3459 and #4191: a big model on a slow disk was stopped at 300 s while the engine went on reading. */ 739test ( 'packaged: a big model is waited for by its size, inside the ceiling of the Control this app has (#3459 #4191)' , async () => { 740const run = async ( cliStartWait , downloadGb ) => { 741const f = packagedFixture ({ ids :[ 'big' ], cliStartWait, extraSizes :[{ id :'big' , model :'BIG' , download_gb :downloadGb , ram_needed_gb :60 , about :'x' , on_this_pc :'fits' , installed :false , images :false , experimental :false }] }); 742try { 743await f . native . refresh ( true ); 744const job = await finish ( f . native , f . native . operation ( 'load' , { model :'big' }). id ); 745assert . equal ( job . state , 'done' , job . error ); 746const start = f . calls . find ( c => c [ 0 ] === 'start' ); 747const saved = f . saved [ 0 ]; 748await f . native . close (); 749return { wait :start [ 2 ], ready :saved . engine_ready_s }; 750} finally { f . cleanup (); } 751}; 752assert . equal (( await run ( 'old' , 180 )). wait , 600 , 'a Control with no long-wait ceiling at all is asked for 600 s, as today' ); 753assert . equal (( await run ( 'split' , 180 )). wait , 600 , 'a Control whose runtime takes 1800 s but whose CLI still stops at 600 is asked for 600 s: the CLI would refuse the start outright (#3949)' ); 754assert . equal (( await run ( 'cli-only' , 180 )). wait , 600 , 'a Control whose CLI takes it but whose operation record still stops at 600 is asked for 600 s too: the record is written before the start runs' ); 755const long = await run ( 'runtime' , 180 ); 756assert . equal ( long . wait , 1720 , '600 s + 8 s per GB above 40 GB, as in strata launch, once the CLI really takes it' ); 757assert . equal ( long . ready , 1780 , 'the server readies after the start gave up, never before it' ); 758const small = await run ( 'runtime' , 20 ); 759assert . equal ( small . wait , 300 , 'a model under 40 GB keeps the 300 s it always had' ); 760assert . equal ( small . ready , undefined , 'nothing is added to a small model’s run config' ); 761const huge = await run ( 'runtime' , 400 ); 762assert . equal ( huge . wait , 1800 , 'and never more than this Control accepts' ); 763}); 764 765test ( 'packaged: a run config that names a longer engine wait is honoured, and never shortened by the app' , async () => { 766const f = packagedFixture ({ ids :[ 'qwen3.6-35b-a3b-ud-q4_k_xl' ], cliStartWait :'runtime' }); 767try { 768writeFileSync ( join ( f . install , 'strata-qwen3.6-35b-a3b-ud-q4_k_xl.json' ), JSON . stringify ({ exe :'component:strata-engine' , tokenizer :join ( f . install , 'tok' ), args :[ '--max-context' , '32768' ], model_name :'Qwen qwen3.6-35b-a3b-ud-q4_k_xl' , port :8080 , engine_ready_s :3600 })); 769await f . native . refresh ( true ); 770const job = await finish ( f . native , f . native . operation ( 'load' , { model :'qwen3.6-35b-a3b-ud-q4_k_xl' }). id ); 771assert . equal ( job . state , 'done' , job . error ); 772assert . equal ( f . calls . find ( c => c [ 0 ] === 'start' )[ 2 ], 1800 , 'the wait of the newest Control, which is what the ceiling allows' ); 773assert . equal ( f . saved [ 0 ]. engine_ready_s , 3600 , 'setup’s own longer wait stays as it is' ); 774} finally { await f . native . close (); f . cleanup (); } 775});