diff --git a/apps/host/server/model-folder.mjs b/apps/host/server/model-folder.mjs new file mode 100644 index 00000000..e123fc1c --- /dev/null +++ b/apps/host/server/model-folder.mjs @@ -0,0 +1,65 @@ +/** + * Where the person's models go: what a picked folder means (0.2.2). + * + * - A drive on its own (D:\ or /) is never filled with model files: it becomes D:\Strata (the data folder) and D:\Strata\Models (the + * model library), which the person is told. + * - The data folder is the person's own choice, so it is written into the settings file Strata Control reads (`data_dir`). Control's + * installer accepts a data folder that is that setting, whatever the folder is called; for a folder it has not been told about it wants a + * name starting with "Strata" (a rule against an AI tool filling an arbitrary folder, tools/strata_mcp.py check_data_dir). + * - The link that makes `/models` and `/Models` one folder is only needed where the file system tells them apart; where it cannot be + * made (exFAT, FAT, a network share) the models simply go in `/models`. + */ + +import { existsSync, mkdirSync, readFileSync, renameSync, symlinkSync, writeFileSync } from 'node:fs'; +import { dirname, join, parse, resolve } from 'node:path'; + +export const isDriveRoot = path => { const p = resolve(path); return p === parse(p).root; }; + +/** The data folder for a picked folder: a drive root gets a `Strata` folder of its own. `{ path, note }`. */ +export function dataFolderFromPick(picked) { + const path = resolve(picked); + if (!isDriveRoot(path)) return { path, note: null }; + const target = join(path, 'Strata'); + return { path: target, note: `${path} is a whole drive, so the models go in ${target}.` }; +} + +/** The model library folder for a picked folder: a drive root gets `Strata/Models`. `{ path, note }`. */ +export function libraryFolderFromPick(picked) { + const path = resolve(picked); + if (!isDriveRoot(path)) return { path, note: null }; + const target = join(path, 'Strata', 'Models'); + return { path: target, note: `${path} is a whole drive, so the models go in ${target}.` }; +} + +/** Make a folder the person asked for; a refusal of the disk is said in words. */ +export function makeFolder(path) { + try { mkdirSync(path, { recursive: true }); } + catch (error) { throw new Error(`Strata could not create ${path} (${error.code === 'EACCES' || error.code === 'EPERM' ? 'access was denied' : error.code === 'ENOSPC' ? 'the drive is full' : error.message}). Choose another folder.`); } + return path; +} + +/** `models` beside `Models`: a link where the two are different names. A disk that cannot hold the link gets a plain `models` folder instead. */ +export function alignModels(data, library, { link = symlinkSync } = {}) { + const models = join(data, 'models'); + if (existsSync(models)) return { data, linked: false }; + try { link(process.platform === 'win32' ? library : 'Models', models, process.platform === 'win32' ? 'junction' : 'dir'); return { data, linked: true }; } + catch { /* exFAT, FAT, NFS, a share: no links there */ } + try { mkdirSync(models, { recursive: true }); } catch { /* the install says what it cannot write */ } + return { data, linked: false }; +} + +/** Write the person's data folder into the Control settings file (merged, atomically); returns whether it changed. Never throws: the folder + * still works for the app, only the Terminal and the installer's own check lose the shared setting. */ +export function rememberDataFolder(file, folder) { + if (!file || !folder) return false; + try { + let current = {}; + try { const parsed = JSON.parse(readFileSync(file, 'utf8').replace(/^\uFEFF/, '')); if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) current = parsed; } catch { /* a new file */ } + if (current.data_dir === folder) return false; + mkdirSync(dirname(file), { recursive: true }); + const temp = `${file}.${process.pid}.tmp`; + writeFileSync(temp, JSON.stringify({ ...current, data_dir: folder }, null, 1) + '\n', { mode: 0o600 }); + renameSync(temp, file); + return true; + } catch { return false; } +} diff --git a/apps/host/server/native-control.mjs b/apps/host/server/native-control.mjs index 060dc64b..bea4b919 100644 --- a/apps/host/server/native-control.mjs +++ b/apps/host/server/native-control.mjs @@ -14,6 +14,7 @@ import os from 'node:os'; import { runtimeVision } from './native-vision.mjs'; import { componentsDirectory, installerEnvironment, controlEnvironment } from './control-runtime.mjs'; import { createEngineStep } from './engine-step.mjs'; +import { dataFolderFromPick, isDriveRoot, makeFolder, rememberDataFolder } from './model-folder.mjs'; import { createAutoTune, writeTuneConfig } from './native-autotune.mjs'; import { plainError, PHASE_TEXT } from './update-messages.mjs'; import { checkRunConfig, describeIssues } from './run-config-check.mjs'; @@ -72,6 +73,8 @@ export function createNativeControl({directory,requests=()=>[],changed=()=>{},sp if(JSON.stringify(next)!==JSON.stringify(config)){try{writeModelConfig(path,next);}catch{/* read-only folder: the load reports it */}}}} function installedInfo(info){if(!info)return info;return {...info,models:(info.models??[]).map(entry=>{const cfg=/^strata-[a-z0-9_-]+\.json$/i.test(entry.config??'')?readJson(entry.configPath??join(repo(),entry.config)):{};const at=cfg.args?.indexOf('--native')??-1,nativeFile=at>=0?cfg.args[at+1]:null;const dataFolder=typeof nativeFile==='string'&&basename(dirname(dirname(nativeFile)))==='models'?dirname(dirname(dirname(nativeFile))):info.data_folder;/* A model the person pointed at their own GGUF folder (not /models/): remembered, so a later load runs setup with that folder again instead of downloading the files (#3199). */const ownGgufDir=typeof nativeFile==='string'&&basename(dirname(dirname(nativeFile)))!=='models'&&existsSync(nativeFile)?dirname(nativeFile):undefined;return {...entry,ownGgufDir,diskGb:entry.diskGb??entry.disk_gb,diskBytes:entry.diskBytes??entry.disk_bytes,sharedBytes:entry.sharedBytes??entry.shared_bytes,removableGb:entry.removableGb??entry.removable_gb,dataFolder,visionPrepared:!!cfg.vision,/* The picture files are prepared when the run config has a vision section: the Models screen then loads straight away instead of running setup again (it used to read 'off' for every model, which sent every load of a picture-capable model through "Apply settings and load", and setup writes the arguments, the draft layer and the reader back each time). */images:cfg.vision?'yes':'off',draftLayer:cfg.mtp!==false,/* Picture reading is ON unless the person turned it off: no saved choice and not loaded is undefined, not false (owner rule: pictures on by default). */visionEnabled:cfg.ui_vision_enabled??(()=>{const running=[...instances.values()].find(i=>i.model===entry.model&&i.status==='ready');return running?!!running.vision:undefined;})(),visionDevice:cfg.ui_vision_device,resourceProfile:cfg.ui_resource_profile,lowRamMode:cfg.args?.includes('--resident-experts')?'resident':cfg.args?.includes('--mmap-experts')?'mmap':'auto'};})};} function installRoot(){const found=[preferences.installDir].find(d=>d&&existsSync(d));if(found)return realpathSync(found);mkdirSync(defaultInstall(),{recursive:true});return realpathSync(defaultInstall());} + /* The settings file Strata Control and the installer read (control-runtime.mjs sets STRATA_SETTINGS_FILE to it): the app's data folder is written there. */ + function controlSettingsFile(){return process.env.STRATA_SETTINGS_FILE||join(dirname(componentsDirectory()),'app-settings.json');} function repo(){if(packaged)return installRoot();const path=realpathSync(preferences.repo);if(!existsSync(join(path,'setup.py'))||!existsSync(join(path,'tools/strata_mcp.py'))||!existsSync(join(path,'serve/server.py')))throw new Error('Choose the Strata repository folder containing setup.py and tools/strata_mcp.py.');return path;} function python(){return resolvePython({repo:preferences.repo});} function send(method,params,timeout=30000){return new Promise((resolve,reject)=>{const id=++seq;const alarm=setTimeout(()=>{pending.delete(id);reject(new Error('The native engine did not respond in time. Check engine logs.'));},timeout);pending.set(id,{resolve,reject,alarm});child.stdin.write(JSON.stringify({jsonrpc:'2.0',id,method,params})+'\n');});} @@ -121,7 +124,7 @@ export function createNativeControl({directory,requests=()=>[],changed=()=>{},sp refreshing=(async()=>{try{const [catalog,status]=packaged?await (async()=>{const s=await tool('strata_status',{hardware:true});const c=await tool('strata_models').catch(error=>({families:[],contexts:[],recommendation:{family:null,model:null},context_note:'',error:error.message}));return [c,s];})():await Promise.all([tool('strata_models'),tool('strata_status',{hardware:true})]);let visionReader=false;if(packaged){try{const e=await control.engine();const root=e?.engine?.root??(e?.engine?.exe?dirname(e.engine.exe):'');visionReader=!!root&&['strata-vision','strata-vision.exe'].some(n=>existsSync(join(root,n)));}catch{visionReader=false;}}const markImages=c=>c&&Array.isArray(c.families)?{...c,families:c.families.map(f=>({...f,imagesCapable:!!f.images,images:!!f.images&&visionReader,sizes:(f.sizes??[]).map(s=>({...s,imagesCapable:!!s.images,images:!!s.images&&visionReader}))}))}:c;snapshot={available:true,repo:packaged?status.installDir:repo(),version:packaged?(control.version??null):await execute('git',['describe','--tags','--always'],repo()).then(s=>s.trim()).catch(()=>null),summary:status.summary,catalog:markEngine(packaged?markImages(catalog):catalog,status.engine),engineInfo:{version:status.engine?.version??null,source:status.engine?.source??null},visionReader:packaged?visionReader:true,installed:installedInfo(status.installed),hardware:status.hardware,error:null,install:status.install,external:status.running??null,...(packaged?{packaged:true,installDir:status.installDir,engine:status.engine,installerError:status.installerError}:{})};lastRead=Date.now();if(status.install?.running&&![...jobs.values()].some(j=>j.state==='running'))begin('install',j=>watchInstall(j,status.install));}catch(error){snapshot={...snapshot,available:false,error:error.message};lastRead=Date.now();}emit();return state();})().finally(()=>{refreshing=null;});const run=refreshing;if(!force&&snapshot.available){run.catch(()=>{});return state();}return run; } function configure({repository}){if(packaged){if(instances.size||[...jobs.values()].some(j=>j.state==='running'))throw new Error('Unload models and finish operations before changing the installation.');const next=realpathSync(String(repository));if(!statSync(next).isDirectory())throw new Error('Choose the folder that holds your Strata model configurations.');preferences={...preferences,installDir:next};packagedInstall=null;disconnect();writeFileSync(settingsPath,JSON.stringify(preferences,null,2),{mode:0o600});lastRead=0;return refresh(true);}if(instances.size||[...jobs.values()].some(j=>j.state==='running'))throw new Error('Unload models and finish operations before changing the installation.');const next=realpathSync(String(repository));const old=preferences;preferences={...preferences,repo:next};try{repo();}catch(e){preferences=old;throw e;}disconnect();writeFileSync(settingsPath,JSON.stringify(preferences,null,2),{mode:0o600});lastRead=0;return refresh(true);} - function setDownloadDirectory(input){if([...jobs.values()].some(j=>j.state==='running'))throw new Error('Finish the current operation before changing the folder.');if(typeof input!=='string'||!input.trim()||/[\r\n\0]/.test(input))throw new Error('Choose a model folder.');const path=realpathSync(input.trim());if(!statSync(path).isDirectory())throw new Error('Choose a folder, not a file.');accessSync(path,constants.R_OK|constants.W_OK);preferences={...preferences,downloadDirectory:path};writeFileSync(settingsPath,JSON.stringify(preferences,null,2),{mode:0o600});emit();return state();} + function setDownloadDirectory(input){if([...jobs.values()].some(j=>j.state==='running'))throw new Error('Finish the current operation before changing the folder.');if(typeof input!=='string'||!input.trim()||/[\r\n\0]/.test(input))throw new Error('Choose a model folder.');let path;try{path=realpathSync(input.trim());}catch{throw new Error('That folder does not exist. Check the path, or that the drive is connected.');}if(!statSync(path).isDirectory())throw new Error('Choose a folder, not a file.');/* A whole drive (D:\) is not filled with models: D:\Strata is made and used. */if(isDriveRoot(path)){const own=dataFolderFromPick(path).path;makeFolder(own);path=realpathSync(own);}try{accessSync(path,constants.R_OK|constants.W_OK);}catch{throw new Error('Strata cannot write in that folder. Choose a folder you can save files in.');}preferences={...preferences,downloadDirectory:path};/* the same choice for the installer and the Terminal: the Control settings file */if(packaged)rememberDataFolder(controlSettingsFile(),path);writeFileSync(settingsPath,JSON.stringify(preferences,null,2),{mode:0o600});emit();return state();} function recordFile(model){if(!safeId(model))throw new Error('Choose an installed model.');const entry=snapshot.installed?.models?.find(i=>i.model===model);if(!entry)throw new Error('This model is not installed. Download and prepare it first.');const path=realpathSync(entry.configPath??join(repo(),entry.config));if(dirname(path)!==repo()||!/^strata-[a-z0-9_-]+\.json$/i.test(basename(path)))throw new Error('The model configuration is outside the Strata installation.');return {entry,path};} function record(model){const {entry,path}=recordFile(model);return {entry,path,config:readJson(path)};} /* The text file a power user edits by hand: where it is and what is wrong in it (run-config-check.mjs). */ @@ -133,7 +136,7 @@ export function createNativeControl({directory,requests=()=>[],changed=()=>{},sp function save(model,{values,mcp}){if([...jobs.values()].some(j=>j.state==='running'))throw new Error('Finish the current operation before saving native settings.');const {path,config}=record(model);if(busy(requests().filter(r=>instances.get(r.instanceId)?.model===model)))throw new Error('Finish this model’s requests before saving its settings.');const patch=validateNativeSettings(values??{}),next={...config};for(const field of nativeFields())if(field.supported===false&&patch[field.id]!=null&&JSON.stringify(patch[field.id])!==JSON.stringify(config[field.id]))throw new Error(`${field.label} needs a newer native Strata installation. Update it in Settings → About.`);for(const [key,value] of Object.entries(patch)){if(key==='env')next.env=restoreNativeEnv(value,config.env??{});else if(value===null)delete next[key];else next[key]=value;}if('vram_reserve_mib' in patch){const base=captureUserArgs(next);const mine=setUserFlag(base,'--vram-reserve-mib',patch.vram_reserve_mib);if(mine.length)next[USER_KEY]=mine;else delete next[USER_KEY];next.args=mine.length&&patch.vram_reserve_mib!==null?mergeUserArgs(next.args??[],mine):setUserFlag(next.args??[],'--vram-reserve-mib',null);delete next.ui_reserve_applied;if(Array.isArray(next.args_written))next.args_written=[...next.args];}if(mcp!==undefined){next.mcp_servers=validateMcpServers(mcp,config.mcp_servers??config.mcpServers??{});delete next.mcpServers;}copyFileSync(path,path+'.bak');chmodSync(path+'.bak',0o600);const temp=path+'.ui-tmp';writeFileSync(temp,JSON.stringify(next,null,2)+'\n',{mode:0o600});renameSync(temp,path);return settings(model);} function previousEngine(){if(packaged)return !!snapshot.engine?.previous;try{const data=snapshot.installed?.data_dir??snapshot.installed?.data_folder;return [join(preferences.repo,'engine/.previous'),data&&join(data,'engine/.previous')].filter(Boolean).some(existsSync)&&readFileSync(join(preferences.repo,'setup.py'),'utf8').includes('--rollback-engine');}catch{return false;}} async function plan(args){await refresh();if(!snapshot.available)throw new Error(snapshot.error);return tool('strata_install',installArgs(args,false));} - function installArgs(args,confirm){const allowed=['family','model','context','vision','kv','gpu','gpus','backend','low_ram','port','gguf_dir'];const size=snapshot.catalog?.families?.find(f=>f.family===args.family)?.sizes.find(s=>s.model===args.model);const existing=snapshot.installed?.models?.find(m=>m.model===size?.id);/* A model adopted from the person's own GGUF folder reports that folder's parent as its data folder: it is theirs, not Strata's, so a second preparation never uses it for the packs. */if(args.gguf_dir===undefined&&existing?.ownGgufDir)args={...args,gguf_dir:existing.ownGgufDir};const data=nativeDataDirectory(args.data_dir??(args.gguf_dir?undefined:existing?.dataFolder)??preferences.downloadDirectory??modelsDirectory?.());return {...(data?{data_dir:data}:{}),...Object.fromEntries(allowed.filter(k=>args[k]!==undefined).map(k=>[k,args[k]])),/* One card is "gpu", never "gpus" (the installer wants two or more there: "Gpus='0' is not valid", #3203). */...(/^\d{1,2}$/.test(String(args.gpus??''))?{gpu:Number(args.gpus),gpus:undefined}:{}),/* Picture reading is only downloaded when it was asked for (Read pictures); the default stays the smaller download. */...(packaged&&args.vision===undefined?{vision:'no'}:{}),confirm};} + function installArgs(args,confirm){const allowed=['family','model','context','vision','kv','gpu','gpus','backend','low_ram','port','gguf_dir'];const size=snapshot.catalog?.families?.find(f=>f.family===args.family)?.sizes.find(s=>s.model===args.model);const existing=snapshot.installed?.models?.find(m=>m.model===size?.id);/* A model adopted from the person's own GGUF folder reports that folder's parent as its data folder: it is theirs, not Strata's, so a second preparation never uses it for the packs. */if(args.gguf_dir===undefined&&existing?.ownGgufDir)args={...args,gguf_dir:existing.ownGgufDir};const data=nativeDataDirectory(args.data_dir??(args.gguf_dir?undefined:existing?.dataFolder)??preferences.downloadDirectory??modelsDirectory?.());if(data&&packaged&&!args.data_dir)rememberDataFolder(controlSettingsFile(),data);return {...(data?{data_dir:data}:{}),...Object.fromEntries(allowed.filter(k=>args[k]!==undefined).map(k=>[k,args[k]])),/* One card is "gpu", never "gpus" (the installer wants two or more there: "Gpus='0' is not valid", #3203). */...(/^\d{1,2}$/.test(String(args.gpus??''))?{gpu:Number(args.gpus),gpus:undefined}:{}),/* Picture reading is only downloaded when it was asked for (Read pictures); the default stays the smaller download. */...(packaged&&args.vision===undefined?{vision:'no'}:{}),confirm};} function begin(kind,run,metadata={}){if([...jobs.values()].some(j=>j.state==='running'))throw new Error('Finish or cancel the current engine operation first.');const job={id:randomUUID(),kind,state:'running',startedAt:new Date().toISOString(),summary:'Starting…',result:null,error:null,...metadata};for(const [id,old] of jobs)if(jobs.size>=12&&old.state!=='running')jobs.delete(id);jobs.set(job.id,job);emit();void run(job).then(result=>{if(job.state==='running'){Object.assign(job,{state:'done',summary:result?.summary??'Complete',result,finishedAt:new Date().toISOString()});}}).catch(error=>{if(job.state==='running')Object.assign(job,{state:'error',error:error.message,summary:error.message,...(error.details?{details:error.details}:{}),finishedAt:new Date().toISOString()});}).finally(()=>{if(job.kind.startsWith('update')||job.kind==='rollback')disconnect();lastRead=0;emit();void refresh(true);});return state().jobs.find(j=>j.id===job.id);} async function watchInstall(job,current){job.summary=current.step??'Resuming preparation status…';job.result={install:current};while(job.state==='running'){await new Promise(r=>setTimeout(r,1500));const result=await tool('strata_install');job.summary=result.summary;job.result=result;emit();if(!result.install?.running){if(result.install?.exit_code!==0)throw new Error(result.install?.error??'Preparation did not finish. Retry to reuse completed files.');return {summary:'Model preparation complete. Load it from Models.'};}}} diff --git a/apps/host/server/store.mjs b/apps/host/server/store.mjs index 2b9ea02b..907f37fc 100644 --- a/apps/host/server/store.mjs +++ b/apps/host/server/store.mjs @@ -1,6 +1,7 @@ import { migrateSetup } from './setup.mjs'; /** Tiny persisted state for the host: one JSON file under STRATA_HOME. */ import { accessSync, constants, mkdirSync, readFileSync, writeFileSync, existsSync, realpathSync, readdirSync, statSync, statfsSync, symlinkSync } from 'node:fs'; +import { alignModels, isDriveRoot, libraryFolderFromPick, makeFolder } from './model-folder.mjs'; import { dirname, isAbsolute, join, parse, relative, resolve } from 'node:path'; import { homedir } from 'node:os'; import { defaultModelDirectory } from './model-directory-default.mjs'; @@ -75,12 +76,9 @@ export function nativeDataDirectory(input) { if(!input||process.env.STRATA_MODELS_DIR||!existsSync(DEFAULT_MODELS_DIRECTORY))return input; const root=realpathSync(DEFAULT_MODELS_DIRECTORY); if(resolve(input)!==resolve(DEFAULT_MODELS_DIRECTORY)&&realpathSync(input)!==root)return input; - const data=dirname(root),nativeModels=join(data,'models'); - // On Windows/macOS case-insensitive volumes, Models already resolves as models. - // On Linux, a relative alias keeps the native layout inside the requested folder. - if(!existsSync(nativeModels))symlinkSync(process.platform==='win32'?root:'Models',nativeModels,process.platform==='win32'?'junction':'dir'); - const a=statSync(nativeModels),b=statSync(root); - if(realpathSync(nativeModels)!==root&&!(a.ino&&a.ino===b.ino&&a.dev===b.dev))throw new Error('The default model folder conflicts with another library. Choose a different folder.'); + const data=dirname(root); + // `models` and `Models` are one folder on Windows and macOS; where they are not (Linux) a link makes them one, and where no link can be made (exFAT, a share) models go in `models`. + alignModels(data,root); return data; } @@ -95,6 +93,8 @@ export function validateModelsDirectory(input) { if (!statSync(directory).isDirectory()) throw new Error(); accessSync(directory, constants.R_OK | constants.W_OK | constants.X_OK); } catch { throw new Error('This folder is unavailable or not writable. Check the path and folder permissions.'); } + // A whole drive (D:\) is not filled with models: D:\Strata\Models is made and used. + if (isDriveRoot(directory)) directory = realpathSync(makeFolder(libraryFolderFromPick(directory).path)); const data = realpathSync(DATA_DIR); const contains = (parent, child) => { const r = relative(parent, child); return !r || !r.startsWith('..') && !isAbsolute(r); }; if (directory === parse(directory).root || contains(directory, data) || contains(data, directory)) throw new Error('Choose a separate folder for models, outside Strata’s app data.'); diff --git a/apps/host/tests/model-directory.test.mjs b/apps/host/tests/model-directory.test.mjs index 64403eb7..ecbc3ac4 100644 --- a/apps/host/tests/model-directory.test.mjs +++ b/apps/host/tests/model-directory.test.mjs @@ -32,7 +32,7 @@ test('directory preferences survive restart, scope demo installs, and reject bad import assert from 'node:assert/strict';import {join} from 'node:path';import {existsSync} from 'node:fs'; const store=await import('./server/store.mjs');const models=await import('./server/models.mjs'); const original=store.MODELS_DIR,id='qwen38-q4_k_m';store.update({installed:{[id]:{simulated:true}},setup:{complete:true,step:4},serving:{apiKey:'fixture-only-key'}}); - for(const path of ['relative/path',join(store.HOME,'missing'),store.DATA_DIR,store.HOME,'/'])assert.throws(()=>store.validateModelsDirectory(path)); + for(const path of ['relative/path',join(store.HOME,'missing'),store.DATA_DIR,store.HOME])assert.throws(()=>store.validateModelsDirectory(path)); assert.equal(store.MODELS_DIR,original);assert.equal(existsSync(join(store.HOME,'missing')),false); store.changeModelsDirectory(${JSON.stringify(next)});models.init({send(){}}); assert.equal(models.isOnDisk(id),false);assert.equal(store.get().setup.complete,true);assert.equal(store.get().serving.apiKey,'fixture-only-key'); diff --git a/apps/host/tests/model-folder.test.mjs b/apps/host/tests/model-folder.test.mjs new file mode 100644 index 00000000..1cb40c89 --- /dev/null +++ b/apps/host/tests/model-folder.test.mjs @@ -0,0 +1,99 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { execFileSync, spawnSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join, parse } from 'node:path'; +import { alignModels, dataFolderFromPick, isDriveRoot, libraryFolderFromPick, makeFolder, rememberDataFolder } from '../server/model-folder.mjs'; +import { nativeFixture } from './helpers/native-fixture.mjs'; + +const scratch = t => { const dir = realpathSync(mkdtempSync(join(tmpdir(), 'strata-folder-'))); t.after(() => rmSync(dir, { recursive: true, force: true })); return dir; }; + +test('a whole drive is not filled with models: it becomes Strata (data) and Strata/Models (library)', () => { + const root = parse(tmpdir()).root; + assert.equal(isDriveRoot(root), true); + assert.equal(isDriveRoot(tmpdir()), false); + assert.deepEqual(dataFolderFromPick(root), { path: join(root, 'Strata'), note: `${root} is a whole drive, so the models go in ${join(root, 'Strata')}.` }); + assert.equal(libraryFolderFromPick(root).path, join(root, 'Strata', 'Models')); + assert.deepEqual(dataFolderFromPick(tmpdir()), { path: tmpdir(), note: null }); + assert.deepEqual(libraryFolderFromPick(tmpdir()), { path: tmpdir(), note: null }); +}); + +test('a folder that cannot be made is said in words', t => { + const dir = scratch(t); + writeFileSync(join(dir, 'a-file'), 'x'); + assert.throws(() => makeFolder(join(dir, 'a-file', 'Strata')), /Strata could not create .*Choose another folder\./); + assert.equal(makeFolder(join(dir, 'new', 'Strata')), join(dir, 'new', 'Strata')); + assert.equal(existsSync(join(dir, 'new', 'Strata')), true); +}); + +test('models and Models are one folder; a disk that cannot hold the link gets a plain models folder', t => { + const dir = scratch(t), library = join(dir, 'Models'); + mkdirSync(library); + const refuses = () => { throw Object.assign(new Error('EPERM: operation not permitted, symlink'), { code: 'EPERM' }); }; + const result = alignModels(dir, library, { link: refuses }); + assert.equal(result.linked, false); + assert.equal(existsSync(join(dir, 'models')), true); + // where the names are one folder already (Windows, macOS) nothing is made; where a link works it is used + const other = scratch(t); + const calls = []; + assert.equal(alignModels(other, join(other, 'Models'), { link: (...args) => { calls.push(args); } }).linked, true); + assert.equal(calls.length, 1); + assert.equal(calls[0][1], join(other, 'models')); +}); + +test('the data folder is written into the Control settings file, merged and only when it changes', t => { + const dir = scratch(t), file = join(dir, 'config', 'app-settings.json'); + assert.equal(rememberDataFolder(file, 'D:\\Strata'), true); + assert.deepEqual(JSON.parse(readFileSync(file, 'utf8')), { data_dir: 'D:\\Strata' }); + writeFileSync(file, JSON.stringify({ installs: ['C:\\x'], data_dir: 'C:\\old' })); + assert.equal(rememberDataFolder(file, 'D:\\Strata'), true); + assert.deepEqual(JSON.parse(readFileSync(file, 'utf8')), { installs: ['C:\\x'], data_dir: 'D:\\Strata' }); + assert.equal(rememberDataFolder(file, 'D:\\Strata'), false); + writeFileSync(file, '\uFEFF{"data_dir":"E:\\\\Strata","installs":[]}'); + assert.equal(rememberDataFolder(file, 'E:\\Strata'), false); + writeFileSync(file, 'not json'); + assert.equal(rememberDataFolder(file, 'F:\\Strata'), true); + assert.equal(rememberDataFolder('', 'F:\\Strata'), false); +}); + +test('choosing a folder that is gone or cannot be written says so in words', async t => { + const f = nativeFixture(); + t.after(() => f.cleanup()); + await f.control.refresh(); + assert.throws(() => f.control.setDownloadDirectory(join(f.repo, 'not-there')), /That folder does not exist\. Check the path, or that the drive is connected\./); + assert.throws(() => f.control.setDownloadDirectory(join(f.repo, 'setup.py')), /Choose a folder, not a file\./); +}); + +test('Windows: a drive picked as D:\\ becomes D:\\Strata, and the person is not asked for a different folder', { skip: process.platform !== 'win32' }, async t => { + const dir = scratch(t); + const letter = 'QRSTUVWXYZ'.split('').find(l => !existsSync(`${l}:\\`)); + assert.ok(letter, 'a free drive letter'); + execFileSync('subst', [`${letter}:`, dir]); + t.after(() => { try { execFileSync('subst', [`${letter}:`, '/D']); } catch { /* gone */ } }); + const f = nativeFixture(); + t.after(() => f.cleanup()); + await f.control.refresh(); + f.control.setDownloadDirectory(`${letter}:\\`); + const shown = f.control.state().downloadDirectory; + assert.equal(shown.toLowerCase(), `${letter}:\\Strata`.toLowerCase()); + assert.equal(existsSync(join(dir, 'Strata')), true); + assert.equal(readdirNames(dir).includes('Strata'), true); +}); + +function readdirNames(dir) { return execFileSync('cmd', ['/c', 'dir', '/b', dir]).toString().split(/\r?\n/).filter(Boolean); } + +test('Windows: the model library on a whole drive goes to X:\\Strata\\Models', { skip: process.platform !== 'win32' }, t => { + const dir = scratch(t), home = scratch(t); + const letter = 'QRSTUVWXYZ'.split('').find(l => !existsSync(`${l}:\\`)); + execFileSync('subst', [`${letter}:`, dir]); + t.after(() => { try { execFileSync('subst', [`${letter}:`, '/D']); } catch { /* gone */ } }); + const cwd = fileURLToPath(new URL('..', import.meta.url)); + const drive = `${letter}:\\`; + const run = spawnSync(process.execPath, ['--input-type=module', '-e', `const store=await import('./server/store.mjs');console.log(store.validateModelsDirectory(${JSON.stringify(drive)}));`], + { cwd, env: { ...process.env, STRATA_HOME: home, STRATA_MODELS_DIR: join(home, 'models') }, encoding: 'utf8' }); + assert.equal(run.status, 0, run.stderr); + assert.equal(run.stdout.trim().toLowerCase(), `${drive}Strata\\Models`.toLowerCase()); + assert.equal(existsSync(join(dir, 'Strata', 'Models')), true); +}); diff --git a/docs/DOCKER.md b/docs/DOCKER.md index c87dafe7..dc8ce616 100644 --- a/docs/DOCKER.md +++ b/docs/DOCKER.md @@ -134,6 +134,9 @@ variable; the model and tuning stay in `/data`. Anything else: `docker exec stra What is in the volume `/data`: `Strata-data/` (model files, the prepared pack), `state/` (presets, tuning, the key), `install/` (run settings and logs), `home/`. The engine and the Terminal are in the image, so updating the image never touches your files. +To keep it all on a big disk, mount a folder there instead of the named volume: `-v /mnt/big/strata:/data` (the folder must be writable by the +container's user). Nothing large is written anywhere else in the container. + ### Files you already have Mount the folder with the model's GGUF files read-only and name it: `-v /srv/gguf/IQ3_XXS:/models:ro -e MODEL_PATH=/models diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 80db9797..d860d042 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -305,6 +305,26 @@ says so and starts the engine you had. GitHub at the commit `LLAMA_CPP_COMMIT` in `setup.py` names. Put that commit's tree in `third_party/llama.cpp` yourself (from a mirror or another PC) and setup uses it instead (#585). +### Keeping the models on another disk + +The model files are the only big part (70 to 190 GB). Put them where there is room; the app and the Terminal use the same folder. + +- **In the app:** the first screen shows the model folder with a button to change it, and Settings > Storage changes it + later. Pick a folder you can save files in; a whole drive (`D:\`) becomes `D:\Strata`, which Strata makes and says so. + Models you already have in the old folder stay there and keep working; nothing is moved or deleted for you. +- **In the Terminal:** `strata launch --data-dir D:\Strata` once. The folder is remembered for the next `strata launch` + and `strata models list`. Until you give it one, the Terminal uses the folder the app chose; the app keeps its own setting + (Settings > Storage) and does not read the Terminal's. `--move-data` moves the models you already have into it; without it they stay where they + are and the new folder gets the new downloads. Use your own files in place with `strata launch --model-path FOLDER` (a + folder of shards, a folder above it, or one `.gguf` file); a folder that holds several sizes needs the one `.gguf` file. +- **Drives that are not NTFS** (exFAT, FAT32, a network share) work: Strata writes straight into `\models` and makes no + link there. +- **What stays on C:** the engine (`%LOCALAPPDATA%\Strata\components`, 1 to 2 GB per version; `STRATA_COMPONENTS_DIR` moves it), + the app itself (the installer asks), your settings, chat history, tuning results and logs (`%LOCALAPPDATA%\Strata`, + `%APPDATA%\Strata`; a few MB). Nothing else large: downloads, the prepared model data (`packs`) and the draft layer (`mtp`) are + written in the model folder, and so are the half-finished files of an interrupted download. +- **Docker:** `-v /mnt/big/strata:/data` (a folder on the big disk) instead of the volume `strata-data`; everything above lives in it. + ## Setup's questions These are the questions of the installer from source (`START-HERE.bat`, `./setup.sh`). The app's first screen and diff --git a/strata_cli/launch/runner.py b/strata_cli/launch/runner.py index aae8ed96..8c95b06e 100644 --- a/strata_cli/launch/runner.py +++ b/strata_cli/launch/runner.py @@ -510,6 +510,8 @@ class Runner: args += ["--data-dir", str(data)] if self.move_data and s.data_dir: # only when the person said so: setup moves nothing otherwise args.append("--move-data") + if s.data_dir: # a folder the person chose is remembered for the next start and for the app + args.append("--set-default-data-dir") if s.model_path: p = Path(s.model_path) args += ["--gguf-dir", str(p if p.is_dir() else p.parent)] @@ -517,8 +519,9 @@ class Runner: args += ["--gpus", ",".join(str(g) for g in s.gpus)] if len(s.gpus) > 1 else ["--gpu", str(s.gpus[0])] if sys.platform == "darwin": # a Mac's setup (Control mode, strata_control/mac_fit.py) takes fewer options (no --kv, --gpu, speed projection) - keep = {"--yes", "--no-start", "--family", "--model", "--context", "--vision", "--data-dir", "--gguf-dir", "--move-data"} - flags = ("--yes", "--no-start", "--move-data") + keep = {"--yes", "--no-start", "--family", "--model", "--context", "--vision", "--data-dir", "--gguf-dir", "--move-data", + "--set-default-data-dir"} + flags = ("--yes", "--no-start", "--move-data", "--set-default-data-dir") out, i = [], 0 while i < len(args): if args[i] in keep: diff --git a/tests/terminal/test_launch.py b/tests/terminal/test_launch.py index 78c718fe..67547814 100644 --- a/tests/terminal/test_launch.py +++ b/tests/terminal/test_launch.py @@ -407,11 +407,13 @@ class SetupArgs(unittest.TestCase): ["--gpu", "1"]): self.assertIn(pair[0], a) self.assertEqual(a[a.index(pair[0]) + 1], pair[1]) + self.assertIn("--set-default-data-dir", a) # a folder the person chose is the remembered one (the app and the next start use it) def test_a_mac_gets_only_the_options_its_setup_knows(self): with mock.patch.object(sys, "platform", "darwin"): a = self.runner(data_dir="/d", gpus=[0]) - self.assertEqual(a, ["--yes", "--no-start", "--family", "qwen", "--model", "IQ3_XXS", "--context", "32768", "--vision", "no", "--data-dir", "/d"]) + self.assertEqual(a, ["--yes", "--no-start", "--family", "qwen", "--model", "IQ3_XXS", "--context", "32768", "--vision", "no", "--data-dir", "/d", + "--set-default-data-dir"]) class AdoptRules(unittest.TestCase): diff --git a/tools/test_strata_mcp.py b/tools/test_strata_mcp.py index 81ed0135..1e923413 100644 --- a/tools/test_strata_mcp.py +++ b/tools/test_strata_mcp.py @@ -346,6 +346,17 @@ class Validation(FakeRoot): for d in ok: self.assertEqual(M.norm(self.tools.check_data_dir(str(d))), M.norm(d)) + def test_the_persons_own_data_folder_is_accepted_whatever_it_is_called(self): + """The app writes the folder the person chose into the settings file (apps/host model-folder.mjs); an AI tool naming a new + folder still needs a Strata... name.""" + chosen = self.root.parent / "my-big-disk" + chosen.mkdir() + self.assertRejected("strata_install", {"data_dir": str(chosen)}, "named Strata") + settings = self.tools.s.settings_path() + settings.parent.mkdir(parents=True, exist_ok=True) + settings.write_text(json.dumps({"data_dir": str(chosen)})) + self.assertEqual(M.norm(self.tools.check_data_dir(str(chosen))), M.norm(chosen)) + def test_other_tools_arguments(self): self.assertRejected("strata_start", {"model": "../../etc/passwd"}, "not valid") self.assertRejected("strata_start", {"model": "q2_0"}, "no model is installed")