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++

bundled engine: CUDA 13 and CUDA 12 builds of one version install side by side, only the ones the NVIDIA driver can run; fix the duplicate 'ready' parameter that made native-control.mjs a syntax error

Niko1221395ed1ac5498
diff --git a/apps/host/server/bundled-engine.mjs b/apps/host/server/bundled-engine.mjs
index fccf40213..12cea7ac6 100644
--- a/apps/host/server/bundled-engine.mjs
+++ b/apps/host/server/bundled-engine.mjs
@@ -9,7 +9,8 @@
* The copy is made under a hidden temporary name and renamed into place, so an interrupted run leaves nothing half-installed
* (the next start removes the leftover and tries again).
*/
-import { createReadStream, existsSync } from 'node:fs';
+import { createReadStream, existsSync, readFileSync } from 'node:fs';
+import { execFileSync } from 'node:child_process';
import { copyFile, mkdir, readdir, readFile, rename, rm, stat } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import { dirname, join, resolve, sep } from 'node:path';
@@ -45,6 +46,27 @@ function sha256(path) {
});
}
+/**
+ * The NVIDIA driver version of this PC (the oldest when several cards disagree), or null when there is none or it cannot be read.
+ * Linux: the kernel module's own file; anywhere: nvidia-smi. Never throws.
+ */
+export function nvidiaDriverVersion() {
+ try {
+ const text = readFileSync('/proc/driver/nvidia/version', 'utf8');
+ const found = /Kernel Module\s+(\d+(?:\.\d+)+)/.exec(text);
+ if (found) return found[1];
+ } catch { /* not Linux, or no NVIDIA driver */ }
+ try {
+ const out = execFileSync('nvidia-smi', ['--query-gpu=driver_version', '--format=csv,noheader'], { encoding: 'utf8', timeout: 8000, windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] });
+ const versions = out.split(/\r?\n/).map(line => line.trim()).filter(line => /^\d+(\.\d+)+$/.test(line));
+ if (versions.length) return versions.sort(compareVersions)[0];
+ } catch { /* no nvidia-smi */ }
+ return null;
+}
+
+/** One engine can ship as several builds of the same backend (CUDA 13 and CUDA 12 differ in the oldest driver they run on). */
+const variantOf = manifest => `${manifest.platform}/${manifest.backend}/${manifest.min_driver?.nvidia ?? ''}`;
+
/** Does every file the manifest lists exist with its size (and, when `deep`, its SHA-256)? */
export async function packageIsWhole(folder, manifest, { deep = false } = {}) {
for (const item of manifest.files) {
@@ -63,7 +85,7 @@ export async function packageIsWhole(folder, manifest, { deep = false } = {}) {
* Install the bundled engine when it is needed.
* Returns { action: 'installed' | 'repaired' | 'kept' | 'none' | 'refused', folder?, version?, reason? }; never throws.
*/
-export async function installBundledEngine({ bundleDir, components, platform = null, log = () => {} } = {}) {
+export async function installBundledEngine({ bundleDir, components, platform = null, log = () => {}, driver = undefined } = {}) {
try {
if (!bundleDir || !components || !existsSync(bundleDir)) return { action: 'none' };
let packages = [];
@@ -74,13 +96,27 @@ export async function installBundledEngine({ bundleDir, components, platform = n
}
if (platform) packages = packages.filter(p => p.manifest.platform === platform);
if (!packages.length) return { action: 'none' };
- // the newest bundled package of each platform/backend
+ // The CUDA builds the NVIDIA driver here can run: all of them when the driver is unknown or fits several; when none fits, the one with the lowest
+ // requirement, so that Control can name the driver to install (copying the others would only waste disk).
+ const driverHere = driver === undefined ? nvidiaDriverVersion() : driver;
+ const need = item => item.manifest.min_driver?.nvidia ?? null;
+ if (driverHere) {
+ const cuda = packages.filter(p => need(p));
+ const fits = cuda.filter(p => compareVersions(driverHere, need(p)) >= 0);
+ if (cuda.length && !fits.length) {
+ const lowest = [...cuda].sort((a, b) => compareVersions(need(a), need(b)))[0];
+ packages = packages.filter(p => !need(p) || p === lowest);
+ } else {
+ packages = packages.filter(p => !need(p) || fits.includes(p));
+ }
+ }
+ // the newest bundled package of each platform/backend/driver requirement
packages.sort((a, b) => compareVersions(b.manifest.version, a.manifest.version));
const engineRoot = join(components, 'engine');
let last = { action: 'none' };
const done = new Set();
for (const { name, manifest } of packages) {
- const key = `${manifest.platform}/${manifest.backend}`;
+ const key = variantOf(manifest);
if (done.has(key)) continue;
done.add(key);
if (!/^[\w.+-]+$/.test(name)) { last = { action: 'refused', reason: 'the package folder name is not plain' }; continue; }
@@ -90,7 +126,7 @@ export async function installBundledEngine({ bundleDir, components, platform = n
for (const entry of await readdir(engineRoot, { withFileTypes: true })) {
if (!entry.isDirectory() || entry.name.startsWith('.')) continue;
const installed = await readManifest(join(engineRoot, entry.name));
- if (!installed || installed.platform !== manifest.platform || installed.backend !== manifest.backend) continue;
+ if (!installed || variantOf(installed) !== variantOf(manifest)) continue;
if (entry.name === name) sameFolder = installed;
if (!newestInstalled || compareVersions(installed.version, newestInstalled.version) > 0) newestInstalled = installed;
}
diff --git a/apps/host/server/native-control.mjs b/apps/host/server/native-control.mjs
index d72dcbc47..196aa65ac 100644
--- a/apps/host/server/native-control.mjs
+++ b/apps/host/server/native-control.mjs
@@ -21,7 +21,7 @@ const safeId=value=>typeof value==='string'&&/^[a-z0-9_-]{1,80}$/i.test(value);
const busy=requests=>requests.some(r=>['queued','generating'].includes(r.state));
function readJson(path,fallback={}){try{return JSON.parse(readFileSync(path,'utf8').replace(/^\uFEFF/,''));}catch{return fallback;}}
-export function createNativeControl({directory,requests=()=>[],changed=()=>{},spawnProcess=spawn,fetcher=fetch,modelsDirectory,nativeDataDirectory=value=>value,beforeLoad=async()=>{},hardwareProbe=()=>[],control=null,ready=null,ready=null,appUpdater=null,defaultRepo=process.env.STRATA_NATIVE_REPO??resolve(ROOT,'../../Strata')}={}){
+export function createNativeControl({directory,requests=()=>[],changed=()=>{},spawnProcess=spawn,fetcher=fetch,modelsDirectory,nativeDataDirectory=value=>value,beforeLoad=async()=>{},hardwareProbe=()=>[],control=null,ready=null,appUpdater=null,defaultRepo=process.env.STRATA_NATIVE_REPO??resolve(ROOT,'../../Strata')}={}){
mkdirSync(directory,{recursive:true});
const settingsPath=join(directory,'native-host.json');
let preferences=readJson(settingsPath,{repo:defaultRepo});
diff --git a/apps/host/tests/bundled-engine.test.mjs b/apps/host/tests/bundled-engine.test.mjs
index 7a4be1bff..a6e6f93ba 100644
--- a/apps/host/tests/bundled-engine.test.mjs
+++ b/apps/host/tests/bundled-engine.test.mjs
@@ -8,14 +8,14 @@ import { join } from 'node:path';
import { installBundledEngine, compareVersions } from '../server/bundled-engine.mjs';
const sha = buffer => createHash('sha256').update(buffer).digest('hex');
-function makePackage(root, version, { body = 'engine-' + version, backend = 'cuda', platform = 'windows-x86_64' } = {}) {
- const name = `${version}-${platform}-${backend}-cuda13`;
+function makePackage(root, version, { body = 'engine-' + version, backend = 'cuda', platform = 'windows-x86_64', variant = 'cuda13', minDriver = null } = {}) {
+ const name = `${version}-${platform}-${backend}-${variant}`;
const dir = join(root, name);
mkdirSync(dir, { recursive: true });
const files = { 'strata.exe': Buffer.from(body), 'strata_kq.dll': Buffer.from('kq-' + body), 'BUILD.json': Buffer.from('{}') };
for (const [file, data] of Object.entries(files)) writeFileSync(join(dir, file), data);
writeFileSync(join(dir, 'engine-component.json'), JSON.stringify({
- schema_version: 2, id: 'strata-engine', name: 'strata-engine', version, platform, backend, archs: ['sm_86'], build_id: 'x', entrypoint: 'strata.exe',
+ schema_version: 2, id: 'strata-engine', name: 'strata-engine', version, platform, backend, archs: ['sm_86'], ...(minDriver ? { min_driver: { nvidia: minDriver } } : {}), build_id: 'x', entrypoint: 'strata.exe',
files: Object.entries(files).map(([path, data]) => ({ path, sha256: sha(data), bytes: data.length })), signature: null }));
return name;
}
@@ -98,3 +98,31 @@ test('a bundle that does not match its manifest is refused and installs nothing'
assert.equal((await installBundledEngine({ bundleDir: join(t.base, 'nope'), components: t.components })).action, 'none');
} finally { t.cleanup(); }
});
+
+test('CUDA 13 and CUDA 12 builds of one version are different engines: both are installed when the driver runs both', async () => {
+ const t = setup();
+ try {
+ const n13 = makePackage(t.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda13', minDriver: '580.65', body: 'thirteen' });
+ const n12 = makePackage(t.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda12', minDriver: '575.51', body: 'twelve' });
+ assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components, driver: '580.178.04' })).action, 'installed');
+ assert.deepEqual(readdirSync(t.engine).sort(), [n12, n13].sort());
+ assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components, driver: '580.178.04' })).action, 'kept', 'a second start does nothing');
+ } finally { t.cleanup(); }
+});
+
+test('an older driver gets only the build it can run; a driver below both gets the lowest build so the app can name the driver', async () => {
+ const t = setup(), u = setup(), v = setup();
+ try {
+ const n13 = makePackage(t.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda13', minDriver: '580.65' });
+ const n12 = makePackage(t.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda12', minDriver: '575.51' });
+ await installBundledEngine({ bundleDir: t.bundle, components: t.components, driver: '575.57.08' });
+ assert.deepEqual(readdirSync(t.engine), [n12], 'driver 575 runs CUDA 12 only');
+ for (const x of [u]) { makePackage(x.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda13', minDriver: '580.65' }); makePackage(x.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda12', minDriver: '575.51' }); }
+ await installBundledEngine({ bundleDir: u.bundle, components: u.components, driver: '550.120' });
+ assert.deepEqual(readdirSync(u.engine), [n12], 'driver 550 runs neither: the CUDA 12 build is installed so Control can say 575 is needed');
+ // no driver at all (a PC without NVIDIA, or nvidia-smi cannot be read): nothing is held back
+ makePackage(v.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda13', minDriver: '580.65' }); makePackage(v.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda12', minDriver: '575.51' });
+ await installBundledEngine({ bundleDir: v.bundle, components: v.components, driver: null });
+ assert.deepEqual(readdirSync(v.engine).sort(), [n12, n13].sort());
+ } finally { t.cleanup(); u.cleanup(); v.cleanup(); }
+});