Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

App: a model load validates and parses the same captured text of its settings file, so a file changed between two reads cannot start with unchecked settings (PR #3954 by zapabob)

zapabobc7833932a1a3
diff --git a/apps/host/server/native-control.mjs b/apps/host/server/native-control.mjs
index 362304b86..895ec054f 100644
--- a/apps/host/server/native-control.mjs
+++ b/apps/host/server/native-control.mjs
@@ -18,7 +18,7 @@ import { createEngineStep } from './engine-step.mjs';
import { dataFolderFromPick, isDriveRoot, makeFolder, rememberDataFolder, syncDataFolder, terminalSettingsFile } 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';
+import { checkAndParseRunConfig, checkRunConfig, describeIssues } from './run-config-check.mjs';
import { openTextFile } from './open-text-file.mjs';
import { markEngine, engineRefusal } from './engine-version.mjs';
import { friendlyModelName } from './model-names.mjs';
@@ -150,7 +150,7 @@ export function createNativeControl({directory,requests=()=>[],changed=()=>{},sp
function settingsFile(model){const {path}=recordFile(model);return {model,path,name:basename(path),...checkRunConfig(readFileSync(path,'utf8')),note:'Changes you make in the file apply the next time the model starts.'};}
async function openSettingsFile(model,options={}){const info=settingsFile(model);await openTextFile(info.path,options);return info;}
/* A mistake in the file holds the start back with the line, instead of an engine that dies on it. Warnings (an unknown key) never do. */
- function assertSettingsFile(path){const check=checkRunConfig(readFileSync(path,'utf8'));if(!check.ok)throw new Error(`The settings file for this model has a mistake, so the model was not started. ${describeIssues(check.issues,'error').join(' ')}`);}
+ function checkedSettingsConfig(text){const check=checkAndParseRunConfig(text);if(!check.ok)throw new Error(`The settings file for this model has a mistake, so the model was not started. ${describeIssues(check.issues,'error').join(' ')}`);return check.config;}
function settings(model){const {entry,config}=record(model);const values=Object.fromEntries(NATIVE_FIELDS.map(f=>[f.id,config[f.id]??null]));if(typeof values.aliases==='string')values.aliases=values.aliases.split(',').map(v=>v.trim()).filter(Boolean);values.sampling=config.sampling??{};values.args=config.args??[];values.env=publicNativeEnv(config.env??{});return {model,values,mcp:publicMcpServers(config.mcp_servers??config.mcpServers??{}),restartNeeded:true,capabilities:{dsExperts:ownEngine(config)&&dsExpertsSupported(config)},speedSwitches:speedSwitchRows(config.env??{},{deepseek:ownEngine(config),arch:config.arch??null,backend:config.backend??entry?.backend??snapshot.installed?.engine?.backend??null,gpus:snapshot.hardware?.gpus??[]})};}
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};if(patch.ds_experts!=null&&!dsExpertsSupported(config))throw new Error('The Speed choice needs the newer DeepSeek engine. Update the engine in Settings, then choose it.');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;}}
@@ -276,7 +276,7 @@ return {summary:result.summary??'Deleted',removed:result.plan?.gb??null};}
/* Which drafter runs, when it is not the one that was asked for (the engine's own words, "Drafting: MTP (DFlash2 didn't fit)"): under Details of the load. */
async function noteDrafting(job,instance,key){let note='';try{const r=await fetcher(`http://127.0.0.1:${instance.port}/metrics`,{headers:{authorization:`Bearer ${key}`},signal:AbortSignal.timeout(2000)});if(r.ok)note=String((await r.json())?.engine?.drafting_note??'');}catch{/* a convenience */}const lines=[job.drafterNote,note].filter(Boolean);if(lines.length)job.details=[job.details,...lines].filter(Boolean).join('\n');}
async function loadEngine(job,args){
- await refresh(true);if(job.state!=='running')return;const {config,entry,path:configFile}=record(args.model);assertSettingsFile(configFile);if(packaged){const engine=await control.engine();if(!engine.usable)throw new Error(ENGINE_NOT_RECOGNISED+(engine.notice?` ${engine.notice}`:''));}if(!entry.ready){const error=missingFilesError(args.model,entry.missing);append(args.model,error.details[0]);throw error;}
+ await refresh(true);if(job.state!=='running')return;const {entry,path:configFile}=recordFile(args.model);const config=checkedSettingsConfig(readFileSync(configFile,'utf8'));if(packaged){const engine=await control.engine();if(!engine.usable)throw new Error(ENGINE_NOT_RECOGNISED+(engine.notice?` ${engine.notice}`:''));}if(!entry.ready){const error=missingFilesError(args.model,entry.missing);append(args.model,error.details[0]);throw error;}
if(externalRunning().length)throw new Error('A Strata server is already running outside this UI. Stop it before loading here, or connect its API in Settings → Models.');
if(args.multiple&&instances.size>=8)throw new Error('Unload an instance before adding another. Up to eight native instances can run here.');
const {cfg,resources,visionPreferences,parallelChoice,parallelNote:chosenNote}=buildConfig(args,config);
diff --git a/apps/host/server/run-config-check.mjs b/apps/host/server/run-config-check.mjs
index fc8956a89..12ca8b84c 100644
--- a/apps/host/server/run-config-check.mjs
+++ b/apps/host/server/run-config-check.mjs
@@ -134,6 +134,14 @@ export function checkRunConfig(text, { known = KNOWN_KEYS } = {}) {
return { ok: !issues.some(i => i.level === 'error'), issues };
}
+/** Validate and parse one captured text value, so callers use the checked snapshot. */
+export function checkAndParseRunConfig(text, options) {
+ text = String(text ?? '');
+ const checked = checkRunConfig(text, options);
+ if (!checked.ok) return { ...checked, config: null };
+ return { ...checked, config: JSON.parse(text.replace(/^\uFEFF/, '')) };
+}
+
/** The English lines of the issues, `Line 12: …`, for the Details of a refused start. */
export function describeIssues(issues, level) {
return issues.filter(i => !level || i.level === level).map(i => `${i.line ? `Line ${i.line}: ` : ''}${i.message}`);
diff --git a/apps/host/tests/native-settings-file.test.mjs b/apps/host/tests/native-settings-file.test.mjs
index 055110b22..e12cc9db9 100644
--- a/apps/host/tests/native-settings-file.test.mjs
+++ b/apps/host/tests/native-settings-file.test.mjs
@@ -1,11 +1,56 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync, writeFileSync } from 'node:fs';
+import fs from 'node:fs';
+import { syncBuiltinESMExports } from 'node:module';
import { join } from 'node:path';
import { nativeFixture, finishJob } from './helpers/native-fixture.mjs';
const configOf = f => join(f.repo, 'strata-iq2_xs.json');
+for (const validFirst of [true, false]) test(`a load ${validFirst ? 'uses a valid' : 'refuses an invalid'} captured config even if the file changes after reading`, async t => {
+ const f = nativeFixture();
+ try {
+ await f.control.refresh(true);
+ const path = configOf(f), good = JSON.parse(readFileSync(path, 'utf8'));
+ good.parallel = 2;
+ const valid = '\uFEFF' + JSON.stringify(good), invalid = JSON.stringify({ ...good, parallel: 99 });
+ writeFileSync(path, validFirst ? valid : invalid);
+ // Let status discovery finish normally. Replace the real file immediately after
+ // the first read following the model-path check, before a second read can occur.
+ const realpath = fs.realpathSync, read = fs.readFileSync;
+ let capture = false;
+ t.mock.method(fs, 'realpathSync', (...args) => {
+ const result = realpath(...args);
+ if (args[0] === path) capture = true;
+ return result;
+ });
+ t.mock.method(fs, 'readFileSync', (...args) => {
+ const result = read(...args);
+ if (capture && args[0] === path) {
+ capture = false;
+ writeFileSync(path, validFirst ? invalid : valid);
+ }
+ return result;
+ });
+ syncBuiltinESMExports();
+ const done = await finishJob(f.control, f.control.operation('load', { model: 'iq2_xs' }).id);
+ assert.equal(done.state, validFirst ? 'done' : 'error', done.error);
+ if (validFirst) {
+ const instance = f.control.resolveInstance(done.result.instanceId);
+ assert.equal(JSON.parse(read(instance.configPath, 'utf8')).parallel, 2);
+ } else {
+ assert.match(done.error, /"parallel" must be a whole number/);
+ assert.equal(f.control.state().instances.length, 0);
+ assert.equal(f.calls.some(c => c.args.some(a => /serve[/\\]server\.py$/.test(String(a)))), false);
+ }
+ } finally {
+ t.mock.restoreAll();
+ syncBuiltinESMExports();
+ await f.cleanup();
+ }
+});
+
test('the settings file is found, checked, and opened in the text editor', async () => {
const f = nativeFixture();
try {
diff --git a/apps/host/tests/run-config-check.test.mjs b/apps/host/tests/run-config-check.test.mjs
index 5cd00c808..a3fbe0c0e 100644
--- a/apps/host/tests/run-config-check.test.mjs
+++ b/apps/host/tests/run-config-check.test.mjs
@@ -2,7 +2,7 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { build } from 'esbuild';
-import { KNOWN_KEYS, checkRunConfig, describeIssues, nearestKey } from '../server/run-config-check.mjs';
+import { KNOWN_KEYS, checkAndParseRunConfig, checkRunConfig, describeIssues, nearestKey } from '../server/run-config-check.mjs';
import { NATIVE_FIELDS } from '../server/native-settings.mjs';
import { openTextFile, textEditorCommand } from '../server/open-text-file.mjs';
@@ -23,6 +23,20 @@ test('a normal run config has no issues', () => {
assert.deepEqual(checkRunConfig('\uFEFF{}'), { ok: true, issues: [] }, 'Notepad’s byte-order mark is fine');
});
+test('the checked configuration is parsed from the same captured text', () => {
+ const text = '\uFEFF{"parallel":2,"args":["--ple-io","mmap"]}';
+ const checked = checkAndParseRunConfig(text);
+ assert.deepEqual(checked, {
+ ok: true,
+ issues: [],
+ config: { parallel: 2, args: ['--ple-io', 'mmap'] },
+ });
+ const refused = checkAndParseRunConfig('{"parallel":99}');
+ assert.equal(refused.ok, false);
+ assert.equal(refused.config, null);
+ assert.equal(refused.issues[0].code, 'whole-range');
+});
+
test('every key the app edits is a known key', () => {
for (const field of NATIVE_FIELDS) assert.ok(KNOWN_KEYS.has(field.id), field.id);
});