Merge branch 'f-split-tester' into f-split-macapp
This commit is contained in:
commit
13ded2d662
6 files changed
+257
-2
No files matched your search
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"bundle": {
|
||||
"resources": {
|
||||
"resources/engine-bundle": "engine-bundle"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* The engine that ships inside the installer (<install>/engine-bundle/<package folder>, the same layout Strata Control's
|
||||
* packaging/package_engine.py writes: engine-component.json, schema 2, SHA-256 and size for every file).
|
||||
*
|
||||
* On start, when this PC has no engine package for the bundle's platform and backend, or only an older one, the bundled
|
||||
* package is checked against its own manifest and copied into the normal components folder (<components>/engine/<folder>),
|
||||
* where Control and the "engine comes with the model" flow find it like any other installed engine. Nothing is downloaded.
|
||||
* An installed engine of the same version or newer is never touched; a damaged copy of the very same package is repaired.
|
||||
* 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 { copyFile, mkdir, readdir, readFile, rename, rm, stat } from 'node:fs/promises';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { dirname, join, resolve, sep } from 'node:path';
|
||||
|
||||
const MANIFEST = 'engine-component.json';
|
||||
|
||||
const versionKey = value => String(value ?? '').split('.').map(part => Number.parseInt(part, 10) || 0);
|
||||
export function compareVersions(a, b) {
|
||||
const x = versionKey(a), y = versionKey(b);
|
||||
for (let i = 0; i < Math.max(x.length, y.length); i++) if ((x[i] ?? 0) !== (y[i] ?? 0)) return (x[i] ?? 0) > (y[i] ?? 0) ? 1 : -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
async function readManifest(folder) {
|
||||
try {
|
||||
const data = JSON.parse((await readFile(join(folder, MANIFEST), 'utf8')).replace(/^/, ''));
|
||||
if (!data || data.id !== 'strata-engine' || typeof data.version !== 'string' || typeof data.platform !== 'string' || typeof data.backend !== 'string') return null;
|
||||
if (!Array.isArray(data.files) || !data.files.length || typeof data.entrypoint !== 'string') return null;
|
||||
return data;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
function plainRelative(path) {
|
||||
if (typeof path !== 'string' || !path || path.startsWith('/') || path.startsWith('\\') || /^[a-z]:/i.test(path)) return null;
|
||||
const parts = path.split(/[\\/]/);
|
||||
return parts.some(p => !p || p === '..' || p === '.') ? null : parts.join('/');
|
||||
}
|
||||
|
||||
function sha256(path) {
|
||||
return new Promise((resolveHash, reject) => {
|
||||
const hash = createHash('sha256');
|
||||
createReadStream(path).on('data', d => hash.update(d)).on('error', reject).on('end', () => resolveHash(hash.digest('hex')));
|
||||
});
|
||||
}
|
||||
|
||||
/** 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) {
|
||||
const rel = plainRelative(item?.path);
|
||||
if (!rel || !/^[0-9a-f]{64}$/i.test(String(item.sha256 ?? '')) || !Number.isInteger(item.bytes)) return false;
|
||||
const path = join(folder, ...rel.split('/'));
|
||||
let info;
|
||||
try { info = await stat(path); } catch { return false; }
|
||||
if (!info.isFile() || info.size !== item.bytes) return false;
|
||||
if (deep && (await sha256(path)).toLowerCase() !== String(item.sha256).toLowerCase()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = () => {} } = {}) {
|
||||
try {
|
||||
if (!bundleDir || !components || !existsSync(bundleDir)) return { action: 'none' };
|
||||
let packages = [];
|
||||
for (const entry of await readdir(bundleDir, { withFileTypes: true })) {
|
||||
if (!entry.isDirectory() || entry.name.startsWith('.')) continue;
|
||||
const manifest = await readManifest(join(bundleDir, entry.name));
|
||||
if (manifest) packages.push({ name: entry.name, manifest });
|
||||
}
|
||||
if (platform) packages = packages.filter(p => p.manifest.platform === platform);
|
||||
if (!packages.length) return { action: 'none' };
|
||||
// the newest bundled package of each platform/backend
|
||||
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}`;
|
||||
if (done.has(key)) continue;
|
||||
done.add(key);
|
||||
if (!/^[\w.+-]+$/.test(name)) { last = { action: 'refused', reason: 'the package folder name is not plain' }; continue; }
|
||||
// what is installed for the same platform and backend
|
||||
let newestInstalled = null, sameFolder = null;
|
||||
try {
|
||||
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 (entry.name === name) sameFolder = installed;
|
||||
if (!newestInstalled || compareVersions(installed.version, newestInstalled.version) > 0) newestInstalled = installed;
|
||||
}
|
||||
} catch { /* no engine folder yet */ }
|
||||
let repair = false;
|
||||
if (newestInstalled && compareVersions(newestInstalled.version, manifest.version) >= 0) {
|
||||
// an engine of the same version or newer is already there; only a damaged copy of this very package is put right
|
||||
if (sameFolder && !(await packageIsWhole(join(engineRoot, name), sameFolder))) repair = true;
|
||||
else { last = { action: 'kept', version: newestInstalled.version }; continue; }
|
||||
}
|
||||
const source = join(bundleDir, name);
|
||||
if (!(await packageIsWhole(source, manifest, { deep: true }))) {
|
||||
last = { action: 'refused', reason: 'the engine inside the installer does not match its manifest' };
|
||||
log(`bundled engine ${name}: ${last.reason}`);
|
||||
continue;
|
||||
}
|
||||
await mkdir(engineRoot, { recursive: true });
|
||||
const temp = join(engineRoot, `.bundle-${name}.tmp`);
|
||||
await rm(temp, { recursive: true, force: true });
|
||||
try {
|
||||
await mkdir(temp, { recursive: true });
|
||||
for (const item of [...manifest.files.map(f => f.path), MANIFEST]) {
|
||||
const rel = plainRelative(item).split('/');
|
||||
const target = join(temp, ...rel);
|
||||
if (!resolve(target).startsWith(resolve(temp) + sep)) throw new Error('a path leaves the package');
|
||||
await mkdir(dirname(target), { recursive: true });
|
||||
await copyFile(join(source, ...rel), target);
|
||||
}
|
||||
if (!(await packageIsWhole(temp, manifest, { deep: true }))) throw new Error('the copy does not match the manifest');
|
||||
const finalFolder = join(engineRoot, name);
|
||||
if (repair) await rm(finalFolder, { recursive: true, force: true });
|
||||
await rename(temp, finalFolder);
|
||||
} catch (error) {
|
||||
await rm(temp, { recursive: true, force: true }).catch(() => {});
|
||||
last = { action: 'refused', reason: error?.message ?? String(error) };
|
||||
log(`bundled engine ${name}: ${last.reason}`);
|
||||
continue;
|
||||
}
|
||||
last = { action: repair ? 'repaired' : 'installed', folder: join(engineRoot, name), version: manifest.version };
|
||||
log(`bundled engine ${name}: ${last.action}`);
|
||||
}
|
||||
return last;
|
||||
} catch (error) {
|
||||
return { action: 'refused', reason: error?.message ?? String(error) };
|
||||
}
|
||||
}
|
||||
@@ -61,6 +61,7 @@ import { createAppUpdater } from './app-updater.mjs';
|
||||
import { createImageModels } from './image-models.mjs';
|
||||
import { createRuntimeModels } from './runtime-models.mjs';
|
||||
import { createNativeControl } from './native-control.mjs';
|
||||
import { installBundledEngine } from './bundled-engine.mjs';
|
||||
import { findControl, createControl, componentsDirectory } from './control-runtime.mjs';
|
||||
import { forbidden as acquisitionForbidden } from './acquisition.mjs';
|
||||
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
||||
@@ -111,7 +112,11 @@ const control = createControl({ control: CONTROL });
|
||||
// Files a run as administrator made in Strata's own folders (owner Administrators) are given back to the user at every start; best effort, never blocks.
|
||||
if (control && process.platform === 'win32' && !process.env.STRATA_NO_REPAIR_ACCESS) void control.repairAccess().catch(() => {});
|
||||
const appUpdater = createAppUpdater();
|
||||
const native=createNativeControl({control,appUpdater,hardwareProbe:()=>(hardware.cachedHardware()?.gpus??[]).map((g,n)=>({index:g.index??n,vram_gb:g.vramGb})),directory:store.DATA_DIR,modelsDirectory:()=>store.MODELS_DIR,nativeDataDirectory:store.nativeDataDirectory,beforeLoad:async()=>{if(models.REAL&&models.library().loaded){if(serving.state().requests.some(r=>runtimeModels.state().instances.some(i=>i.id===r.instanceId)&&["queued","generating"].includes(r.state)))throw new Error("Finish requests before switching engines.");await models.unload();runtimeState();}},requests:()=>serving.state().requests,changed:value=>hub.send('native',value)});
|
||||
/* The engine that ships inside the installer is put in the components folder when this PC has none (or an older one); no network, hashes checked. The model list waits for it. */
|
||||
const bundledEngine = control && !process.env.STRATA_NO_BUNDLED_ENGINE
|
||||
? installBundledEngine({ bundleDir: process.env.STRATA_ENGINE_BUNDLE_DIR || resolve(HERE, '..', '..', 'engine-bundle'), components: componentsDirectory(), log: line => console.log(line) })
|
||||
: Promise.resolve({ action: 'none' });
|
||||
const native=createNativeControl({control,ready:bundledEngine,appUpdater,hardwareProbe:()=>(hardware.cachedHardware()?.gpus??[]).map((g,n)=>({index:g.index??n,vram_gb:g.vramGb})),directory:store.DATA_DIR,modelsDirectory:()=>store.MODELS_DIR,nativeDataDirectory:store.nativeDataDirectory,beforeLoad:async()=>{if(models.REAL&&models.library().loaded){if(serving.state().requests.some(r=>runtimeModels.state().instances.some(i=>i.id===r.instanceId)&&["queued","generating"].includes(r.state)))throw new Error("Finish requests before switching engines.");await models.unload();runtimeState();}},requests:()=>serving.state().requests,changed:value=>hub.send('native',value)});
|
||||
/* The agent: chats become DeepSeek Harness sessions when that component is installed. It is a separate
|
||||
pinned component (STRATA_HARNESS_DIR), not part of this host, so its absence is a visible capability
|
||||
state and the chat falls back to the direct path with a notice, never silently to another model. */
|
||||
|
||||
@@ -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,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,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});
|
||||
@@ -105,6 +105,7 @@ export function createNativeControl({directory,requests=()=>[],changed=()=>{},sp
|
||||
async function tool(name,args={}){if(packaged&&name==='strata_status')return controlTool(name,args);return mcpCall(name,args);}
|
||||
async function mcpCall(name,args={}){await connect();if(!definitions.some(d=>d.name===name))throw new Error(`This Strata installation does not support ${name}. Update the installation first.`);const result=await send('tools/call',{name,arguments:args},120000);let value=result.structuredContent;if(!value){const text=result.content?.find(c=>c.type==='text')?.text;try{value=JSON.parse(text);}catch{value={summary:text};}}if(result.isError)throw new Error(value?.error??value?.summary??'Native operation failed.');return value;}
|
||||
async function refresh(force=false){
|
||||
if(ready)await ready.catch(()=>{});
|
||||
if(closing)return state();
|
||||
/* A read that is not forced never makes the window wait for the (slow) status of Control: it gets the last state at once, and a stale one
|
||||
is refreshed in the background. Only the first read, and a forced one after an operation, waits. */
|
||||
@@ -124,6 +125,7 @@ export function createNativeControl({directory,requests=()=>[],changed=()=>{},sp
|
||||
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.'};}}}
|
||||
function externalRunning(){return Array.isArray(snapshot.external)?snapshot.external.filter(s=>![...instances.values()].some(i=>i.port===s.port)):[];}
|
||||
async function install(job,args){
|
||||
if(ready)await ready.catch(()=>{});
|
||||
if(job.state!=='running')return;
|
||||
if(externalRunning().length)throw new Error('Stop the native server running outside this UI before preparing files.');
|
||||
if(busy(requests().filter(r=>instances.has(r.instanceId))))throw new Error('Finish or cancel requests before preparing model files.');
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/** The engine inside the installer: installed when missing or older, never over a newer or equal one, never without a matching manifest. */
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, readdirSync, rmSync } from 'node:fs';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { tmpdir } from 'node:os';
|
||||
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`;
|
||||
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',
|
||||
files: Object.entries(files).map(([path, data]) => ({ path, sha256: sha(data), bytes: data.length })), signature: null }));
|
||||
return name;
|
||||
}
|
||||
const setup = () => {
|
||||
const base = mkdtempSync(join(tmpdir(), 'st-bundled-engine-'));
|
||||
const bundle = join(base, 'engine-bundle'), components = join(base, 'components');
|
||||
mkdirSync(bundle); mkdirSync(components);
|
||||
return { base, bundle, components, engine: join(components, 'engine'), cleanup: () => rmSync(base, { recursive: true, force: true }) };
|
||||
};
|
||||
|
||||
test('versions compare by number, not text', () => {
|
||||
assert.equal(compareVersions('0.1.45', '0.1.9'), 1);
|
||||
assert.equal(compareVersions('0.1.45', '0.1.45.1'), -1);
|
||||
assert.equal(compareVersions('0.1.45', '0.1.45'), 0);
|
||||
});
|
||||
|
||||
test('no engine installed: the bundled one is installed, hash-checked, with nothing left over', async () => {
|
||||
const t = setup();
|
||||
try {
|
||||
const name = makePackage(t.bundle, '0.1.45');
|
||||
const result = await installBundledEngine({ bundleDir: t.bundle, components: t.components });
|
||||
assert.equal(result.action, 'installed');
|
||||
assert.equal(readFileSync(join(t.engine, name, 'strata.exe'), 'utf8'), 'engine-0.1.45');
|
||||
assert.ok(existsSync(join(t.engine, name, 'engine-component.json')));
|
||||
assert.deepEqual(readdirSync(t.engine), [name], 'no temporary folder remains');
|
||||
assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'kept', 'a second start does nothing');
|
||||
} finally { t.cleanup(); }
|
||||
});
|
||||
|
||||
test('an older engine is installed: the bundled one is added beside it', async () => {
|
||||
const t = setup();
|
||||
try {
|
||||
const old = makePackage(t.engine, '0.1.44');
|
||||
const name = makePackage(t.bundle, '0.1.45');
|
||||
assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'installed');
|
||||
assert.deepEqual(readdirSync(t.engine).sort(), [old, name].sort());
|
||||
} finally { t.cleanup(); }
|
||||
});
|
||||
|
||||
test('a newer or equal engine is never overwritten or touched', async () => {
|
||||
const t = setup();
|
||||
try {
|
||||
const newer = makePackage(t.engine, '0.1.46', { body: 'mine' });
|
||||
makePackage(t.bundle, '0.1.45');
|
||||
assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'kept');
|
||||
assert.deepEqual(readdirSync(t.engine), [newer]);
|
||||
assert.equal(readFileSync(join(t.engine, newer, 'strata.exe'), 'utf8'), 'mine');
|
||||
// same version, other build: the installed one stays
|
||||
const same = makePackage(t.engine, '0.1.45', { body: 'installed-build' });
|
||||
assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'kept');
|
||||
assert.equal(readFileSync(join(t.engine, same, 'strata.exe'), 'utf8'), 'installed-build');
|
||||
} finally { t.cleanup(); }
|
||||
});
|
||||
|
||||
test('a damaged copy of the same package is put right; an engine for another backend does not count', async () => {
|
||||
const t = setup();
|
||||
try {
|
||||
const name = makePackage(t.bundle, '0.1.45');
|
||||
makePackage(t.engine, '0.1.45');
|
||||
writeFileSync(join(t.engine, name, 'strata.exe'), 'cut'); // wrong size
|
||||
assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'repaired');
|
||||
assert.equal(readFileSync(join(t.engine, name, 'strata.exe'), 'utf8'), 'engine-0.1.45');
|
||||
const t2 = setup();
|
||||
try {
|
||||
makePackage(t2.engine, '0.1.50', { backend: 'cpu' });
|
||||
makePackage(t2.bundle, '0.1.45');
|
||||
assert.equal((await installBundledEngine({ bundleDir: t2.bundle, components: t2.components })).action, 'installed');
|
||||
} finally { t2.cleanup(); }
|
||||
} finally { t.cleanup(); }
|
||||
});
|
||||
|
||||
test('a bundle that does not match its manifest is refused and installs nothing', async () => {
|
||||
const t = setup();
|
||||
try {
|
||||
const name = makePackage(t.bundle, '0.1.45');
|
||||
writeFileSync(join(t.bundle, name, 'strata_kq.dll'), 'kq-engine-0.1.4X'); // same size, other bytes
|
||||
const result = await installBundledEngine({ bundleDir: t.bundle, components: t.components });
|
||||
assert.equal(result.action, 'refused');
|
||||
assert.equal(existsSync(join(t.engine, name)), false);
|
||||
assert.equal((await installBundledEngine({ bundleDir: join(t.base, 'nope'), components: t.components })).action, 'none');
|
||||
} finally { t.cleanup(); }
|
||||
});
|
||||
Reference in new issue
Block a user