diff --git a/apps/host/server/bundled-engine.mjs b/apps/host/server/bundled-engine.mjs index 8d4fdc3f..f3ad0534 100644 --- a/apps/host/server/bundled-engine.mjs +++ b/apps/host/server/bundled-engine.mjs @@ -67,40 +67,70 @@ export function nvidiaDriverVersion() { /** Where the builds are published; the wrong-make sentence points here. */ export const RELEASES_URL = 'https://github.com/Niko1221/Strata/releases'; -/** The PCI vendor ids (`10de`, `1002`, `8086`) of the Windows display adapters; empty off Windows or when the registry cannot be read. */ -export function windowsDisplayVendors({ platform = process.platform, run = execFileSync } = {}) { +/** The PCI vendor ids of the display adapters Windows reports as present right now (PowerShell, Win32_VideoController); empty when that cannot be read. */ +export function windowsPresentVendors({ platform = process.platform, run = execFileSync } = {}) { + const found = new Set(); + if (platform !== 'win32') return found; + try { + const out = run('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', 'Get-CimInstance Win32_VideoController | ForEach-Object { $_.PNPDeviceID }'], { encoding: 'utf8', timeout: 15000, windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] }); + for (const match of String(out).matchAll(/VEN_([0-9A-F]{4})/gi)) found.add(match[1].toLowerCase()); + } catch { /* no PowerShell or a limited account: the registry answer stands */ } + return found; +} + +/** + * The PCI vendor ids (`10de`, `1002`, `8086`) of the Windows display adapters; empty off Windows or when the registry cannot be read. + * The registry keeps the entry of a card that was taken out (an NVIDIA card swapped for a Radeon, #3892): when NVIDIA and AMD are both + * listed, only the makes Windows reports as present now count, the same rule Strata Control uses (engine_component.windows_display_adapters). + */ +export function windowsDisplayVendors({ platform = process.platform, run = execFileSync, present = windowsPresentVendors } = {}) { const found = new Set(); if (platform !== 'win32') return found; try { const out = run('reg', ['query', 'HKLM\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}', '/s', '/v', 'MatchingDeviceId'], { encoding: 'utf8', timeout: 8000, windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] }); for (const match of String(out).matchAll(/VEN_([0-9A-F]{4})/gi)) found.add(match[1].toLowerCase()); } catch { /* no registry access: the tools decide */ } + if (found.has('10de') && found.has('1002')) { + const now = present({ platform, run }); + const kept = new Set([...found].filter(vendor => now.has(vendor))); + if (kept.size) return kept; + } return found; } +const BACKEND_OF_VENDOR = [['10de', 'cuda'], ['1002', 'hip'], ['8086', 'sycl']]; + +/** + * Every engine family this PC's graphics cards can run, in the order NVIDIA, AMD, Intel; STRATA_ENGINE_BACKEND wins. A PC with two + * makes lists both, so a build for either make is accepted (recommend, never force). + */ +export function gpuBackendsHere(env = process.env, { vendors = windowsDisplayVendors() } = {}) { + const forced = String(env.STRATA_ENGINE_BACKEND ?? '').trim().toLowerCase(); + if (forced) return ['cuda', 'hip', 'sycl'].includes(forced) ? [forced] : []; + if (vendors.size) { + const list = BACKEND_OF_VENDOR.filter(([vendor]) => vendors.has(vendor)).map(([, backend]) => backend); + if (list.length) return list; + } + if (nvidiaDriverVersion()) return ['cuda']; + try { + const base = '/sys/class/drm'; + const seen = new Set(); + for (const card of readdirSync(base)) { + if (!/^card\d+$/.test(card)) continue; + try { seen.add(readFileSync(`${base}/${card}/device/vendor`, 'utf8').trim().toLowerCase()); } catch { /* no vendor file */ } + } + const list = [['0x10de', 'cuda'], ['0x1002', 'hip'], ['0x8086', 'sycl']].filter(([vendor]) => seen.has(vendor)).map(([, backend]) => backend); + if (list.length) return list; + } catch { /* not Linux */ } + return []; +} + /** * The engine family this PC's graphics card needs ('cuda', 'hip', 'sycl'), or null when it cannot be told. The same order as Control * (NVIDIA first, then AMD, then Intel); STRATA_ENGINE_BACKEND wins. Reads only the driver and (Linux) the PCI vendor of the display adapters. */ -export function gpuBackendHere(env = process.env, { vendors = windowsDisplayVendors() } = {}) { - const forced = String(env.STRATA_ENGINE_BACKEND ?? '').trim().toLowerCase(); - if (forced) return ['cuda', 'hip', 'sycl'].includes(forced) ? forced : null; - // Windows: the display adapters are the machine's own statement of which cards it has; a leftover nvidia-smi from a card that was - // taken out must not outvote them (#3688). Same order as Control: NVIDIA, then AMD, then Intel. - if (vendors.size) { - for (const [vendor, backend] of [['10de', 'cuda'], ['1002', 'hip'], ['8086', 'sycl']]) if (vendors.has(vendor)) return backend; - } - if (nvidiaDriverVersion()) return 'cuda'; - try { - const base = '/sys/class/drm'; - const vendors = new Set(); - for (const card of readdirSync(base)) { - if (!/^card\d+$/.test(card)) continue; - try { vendors.add(readFileSync(`${base}/${card}/device/vendor`, 'utf8').trim().toLowerCase()); } catch { /* no vendor file */ } - } - for (const [vendor, backend] of [['0x10de', 'cuda'], ['0x1002', 'hip'], ['0x8086', 'sycl']]) if (vendors.has(vendor)) return backend; - } catch { /* not Linux */ } - return null; +export function gpuBackendHere(env = process.env, options = {}) { + return gpuBackendsHere(env, options)[0] ?? null; } const HIP_FAMILIES = [['RX 9000', ['gfx1200', 'gfx1201']], ['RX 7000', ['gfx1100', 'gfx1101', 'gfx1102']], ['RX 6000', ['gfx1030', 'gfx1031']], ['Ryzen AI Max', ['gfx1151']]]; @@ -147,10 +177,12 @@ export async function installBundledEngine({ bundleDir, components, platform = n if (platform) packages = packages.filter(p => p.manifest.platform === platform); if (!packages.length) return { action: 'none' }; // A build for another make of graphics card than this PC has can never run here: it is not unpacked, and the caller says so in plain words. - const wanted = backend === undefined ? gpuBackendHere() : backend; + // A PC with two makes (an AMD card for the model, an NVIDIA card for the desktop) accepts a build for either (#3892). + const wantedList = backend === undefined ? gpuBackendsHere() : (Array.isArray(backend) ? backend : backend ? [backend] : []); + const wanted = wantedList[0] ?? null; if (wanted && ['cuda', 'hip', 'sycl'].includes(wanted)) { const gpu = packages.filter(p => ['cuda', 'hip', 'sycl'].includes(p.manifest.backend)); - const fits = packages.filter(p => !gpu.includes(p) || p.manifest.backend === wanted); + const fits = packages.filter(p => !gpu.includes(p) || wantedList.includes(p.manifest.backend)); if (gpu.length && !fits.length) { const first = gpu.sort((a, b) => compareVersions(b.manifest.version, a.manifest.version))[0].manifest; return { action: 'wrong-make', built: first.backend, here: wanted, notice: wrongMakeNotice(first, wanted) }; diff --git a/apps/host/server/control-runtime.mjs b/apps/host/server/control-runtime.mjs index f0f64dba..2be9e126 100644 --- a/apps/host/server/control-runtime.mjs +++ b/apps/host/server/control-runtime.mjs @@ -74,6 +74,17 @@ export function findControl({ env = process.env, platform = process.platform, ex return control; } +/** + * The environment of the server the host starts from a source checkout (#3851): the key travels in the run configuration, so a + * STRATA_API_KEY the host itself was started with is removed. Setting it to an empty string made serve/server.py stop with + * "an API key was given but it is empty". + */ +export function sourceServerEnvironment(env = process.env) { + const next = { ...env, PYTHONUNBUFFERED: '1', PYTHONUTF8: '1', PYTHONIOENCODING: 'utf-8' }; + delete next.STRATA_API_KEY; + return next; +} + /** The environment Control runs in: nothing from the user's Python, and acquiring forbidden. */ export function controlEnvironment(control, { env = process.env, components = componentsDirectory({ env }), acquire = false } = {}) { const next = { ...env }; diff --git a/apps/host/server/native-control.mjs b/apps/host/server/native-control.mjs index 50dcac5d..362304b8 100644 --- a/apps/host/server/native-control.mjs +++ b/apps/host/server/native-control.mjs @@ -13,7 +13,7 @@ import { USER_KEY, capture as captureUserArgs, merge as mergeUserArgs, setFlag a import { resolvePython, venvPython } from './python-resolver.mjs'; import os from 'node:os'; import { runtimeVision } from './native-vision.mjs'; -import { componentsDirectory, installerEnvironment, controlEnvironment } from './control-runtime.mjs'; +import { componentsDirectory, installerEnvironment, controlEnvironment, sourceServerEnvironment } from './control-runtime.mjs'; import { createEngineStep } from './engine-step.mjs'; import { dataFolderFromPick, isDriveRoot, makeFolder, rememberDataFolder, syncDataFolder, terminalSettingsFile } from './model-folder.mjs'; import { createAutoTune, writeTuneConfig } from './native-autotune.mjs'; @@ -329,7 +329,7 @@ return {summary:result.summary??'Deleted',removed:result.plan?.gb??null};} try{await startWith(retryConfig);append(id,'Strata Control reports the engine is ready (second try).');} catch(second){const said2=humanStartError(second);instance.status='error';instance.error=said2;/* the first stop and the second try stay in job.details; the pop-up shows the last stop once */emit();throw new Error(instance.error);}}} } else { - proc=spawnProcess(py,[join(repo(),'serve/server.py'),'--engine','strata','--config',configPath,'--port',String(cfg.port),'--host','127.0.0.1'],{cwd:repo(),detached:process.platform!=='win32',stdio:['ignore','pipe','pipe'],env:{...process.env,PYTHONUNBUFFERED:'1',PYTHONUTF8:'1',PYTHONIOENCODING:'utf-8',STRATA_API_KEY:''}}); + proc=spawnProcess(py,[join(repo(),'serve/server.py'),'--engine','strata','--config',configPath,'--port',String(cfg.port),'--host','127.0.0.1'],{cwd:repo(),detached:process.platform!=='win32',stdio:['ignore','pipe','pipe'],env:sourceServerEnvironment()}); instances.set(id,instance);processes.set(id,proc);if(!defaultId||!args.multiple||wasDefault)defaultId=id; for(const stream of [proc.stdout,proc.stderr])stream.on('data',chunk=>{const lines=chunk.toString().split('\n').filter(Boolean);lines.forEach(line=>append(id,line.replaceAll(key,'[redacted]')));job.summary=lines.at(-1)??job.summary;emit();}); proc.on('error',error=>{instance.status='error';instance.error=error.message;emit();});proc.on('exit',code=>{if(instances.has(id)&&instance.status!=='unloading'){instance.status='error';instance.error=`Engine stopped${code===null?'':` (exit ${code})`}. Open diagnostics or reload.`;emit();}});emit(); diff --git a/apps/host/tests/bundled-engine.test.mjs b/apps/host/tests/bundled-engine.test.mjs index 74464eed..551c8fe4 100644 --- a/apps/host/tests/bundled-engine.test.mjs +++ b/apps/host/tests/bundled-engine.test.mjs @@ -290,3 +290,31 @@ test('macOS: the engine bundle inside a signed .app stays (deleting it breaks th t.cleanup(); } }); + +test('a card taken out of the PC does not decide the make; two present makes accept a build for either (#3892)', async () => { + const { gpuBackendsHere, windowsDisplayVendors } = await import('../server/bundled-engine.mjs'); + const nl = String.fromCharCode(10); + const registry = 'MatchingDeviceId REG_SZ PCI/VEN_10DE&DEV_2882' + nl + 'MatchingDeviceId REG_SZ PCI/VEN_1002&DEV_7550'; + // the NVIDIA card was swapped for a Radeon: Windows still lists its driver entry, but only the AMD card is present + assert.deepEqual([...windowsDisplayVendors({ platform: 'win32', run: () => registry, present: () => new Set(['1002']) })], ['1002']); + assert.equal(gpuBackendsHere({}, { vendors: new Set(['1002']) })[0], 'hip'); + // both cards present: both makes count, NVIDIA first + assert.deepEqual([...windowsDisplayVendors({ platform: 'win32', run: () => registry, present: () => new Set(['1002', '10de']) })].sort(), ['1002', '10de']); + assert.deepEqual(gpuBackendsHere({}, { vendors: new Set(['10de', '1002']) }), ['cuda', 'hip']); + // present cannot be read: the registry answer stands + assert.equal(windowsDisplayVendors({ platform: 'win32', run: () => registry, present: () => new Set() }).size, 2); + // the second source is only asked when two makes are listed + assert.equal(windowsDisplayVendors({ platform: 'win32', run: () => 'MatchingDeviceId REG_SZ PCI/VEN_1002&DEV_7550', present: () => { throw new Error('must not run'); } }).size, 1); + assert.deepEqual(gpuBackendsHere({ STRATA_ENGINE_BACKEND: 'hip' }, { vendors: new Set(['10de', '1002']) }), ['hip']); + + const t = setup(); + try { + const name = makePackage(t.bundle, '0.2.0', { platform: 'windows-x86_64', backend: 'hip', variant: 'rx9070' }); + const manifest = JSON.parse(readFileSync(join(t.bundle, name, 'engine-component.json'), 'utf8')); + manifest.archs = ['gfx1201']; + writeFileSync(join(t.bundle, name, 'engine-component.json'), JSON.stringify(manifest)); + // an AMD build on a PC with an NVIDIA desktop card and an AMD model card is unpacked, not refused + const result = await installBundledEngine({ bundleDir: t.bundle, components: t.components, backend: ['cuda', 'hip'] }); + assert.equal(result.action, 'installed'); + } finally { t.cleanup(); } +}); diff --git a/apps/host/tests/source-server-env.test.mjs b/apps/host/tests/source-server-env.test.mjs new file mode 100644 index 00000000..21519280 --- /dev/null +++ b/apps/host/tests/source-server-env.test.mjs @@ -0,0 +1,15 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import { sourceServerEnvironment } from '../server/control-runtime.mjs'; + +test('the source-checkout server never gets an empty STRATA_API_KEY (#3851)', async () => { + const env = sourceServerEnvironment({ PATH: 'x', STRATA_API_KEY: 'from-the-shell' }); + assert.equal('STRATA_API_KEY' in env, false); + assert.equal(env.PATH, 'x'); + assert.equal(env.PYTHONUTF8, '1'); + assert.equal('STRATA_API_KEY' in sourceServerEnvironment({}), false); + const control = await readFile(new URL('../server/native-control.mjs', import.meta.url), 'utf8'); + assert.doesNotMatch(control, /STRATA_API_KEY:\s*''/); + assert.match(control, /env:sourceServerEnvironment\(\)/); +});