diff --git a/app/host/src/components/NativeModels.tsx b/app/host/src/components/NativeModels.tsx index 50791e7ba3..03bc5272ee 100644 --- a/app/host/src/components/NativeModels.tsx +++ b/app/host/src/components/NativeModels.tsx @@ -191,6 +191,7 @@ export function NativeModels({ onOtherModels, onReady, onImport, onConnectEngine setDrafting(installed.drafting ?? 'auto'); setLingMtp(installed.lingMtp ?? 'auto'); setLingSkip(installed.lingSkip ?? 'off'); + setQ4Loop(installed.q4Loop === 'on' ? 'on' : 'off'); // the saved Loop shows as saved (a saved On was shown as Off after a restart, and a re-prepare then left it out of setup, which turns it Off) // A model never tuned has no saved choice: keep the page's own (Leave room for my work) rather than flipping to Full performance. if (installed.resourceProfile && !loadedSelected?.resourceProfile) setResourceProfile(installed.resourceProfile); @@ -198,12 +199,13 @@ export function NativeModels({ onOtherModels, onReady, onImport, onConnectEngine else { setLingMtp('auto'); setLingSkip('off'); + setQ4Loop('off'); // a model that is not installed has no saved Loop: Off, never the last model's setDrafting('auto'); // a model that is not installed has no saved choice: Auto (the page never carries the last model's Drafting over to another one) setVision(shownVision(visionPicks.current.get(selectedSize.id), undefined)); if (!contextPicked) // a context the person picked stays; otherwise 128K setContext(defaultContext(contextList)); } - }, [selectedSize?.id, installed?.model, installed?.ready, installed?.context, installed?.contextChosen, installed?.kv, installed?.lowRamMode, installed?.drafting, installed?.resourceProfile]); // drafting too: the saved choice that arrives after the page opened (an app restart) is the one shown, not Auto (0.2.15 release test) + }, [selectedSize?.id, installed?.model, installed?.ready, installed?.context, installed?.contextChosen, installed?.kv, installed?.lowRamMode, installed?.drafting, installed?.q4Loop, installed?.resourceProfile]); // drafting too: the saved choice that arrives after the page opened (an app restart) is the one shown, not Auto (0.2.15 release test) const pickVision = (next: string) => { if (selectedSize) visionPicks.current.set(selectedSize.id, next); setVision(next); }; useEffect(() => { if (jobId && job?.state === 'done' && job.id !== toastedJob && (job.kind === 'load' || job.kind === 'install')) { setToastedJob(job.id); diff --git a/app/host/tests/qwen4-loop-ui-sync.test.mjs b/app/host/tests/qwen4-loop-ui-sync.test.mjs new file mode 100644 index 0000000000..bbb2afc1ad --- /dev/null +++ b/app/host/tests/qwen4-loop-ui-sync.test.mjs @@ -0,0 +1,129 @@ +/** + * 0.2.28, owner's top item "why cant loop be turned off": the Loop choice of the Models page and the app's setup. + * (a) the page shows the SAVED Loop: the select starts from installed.q4Loop (a saved On used to show Off after a restart, because the state started at 'off' and was never synced); + * (b) a re-prepare (context, KV, pictures change) passes `--loop on` to setup when the Loop is On, and nothing when it is Off: setup's Loop is opt-in + * (Control: a setup run that does not say `--loop on` leaves Loop Off), so a re-prepare that forgot it would turn a saved On off. + * The fixture is fix0215-drafting-off.test.mjs's (fake Control, fake installer over MCP). + */ +import './helpers/isolate-profile.mjs'; // a test run never writes the owner's real Strata profile +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { EventEmitter } from 'node:events'; +import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; +import { createNativeControl, MTP_OFFLINE_NOTE } from '../server/native-control.mjs'; + +function fixture({ plan }) { + const root = mkdtempSync(join(tmpdir(), 'strata-fix0215-')); + const install = join(root, 'install'), data = join(root, 'host-data'), home = join(root, 'home'), modelsDir = join(home, 'models'); + for (const d of [install, data, modelsDir]) mkdirSync(d, { recursive: true }); + const earlier = process.env.STRATA_INSTALL_DIR; + process.env.STRATA_INSTALL_DIR = install; + const dataFolder = join(root, 'Strata-data'), modelFolder = join(dataFolder, 'models', 'UD-Q4_K_XL'); + const gguf = join(modelFolder, 'Qwen3.8-Flash-Next-UD-Q4_K_XL-00001-of-00002.gguf'); + mkdirSync(modelFolder, { recursive: true }); + writeFileSync(gguf, 'GGUF'); writeFileSync(gguf.replace('00001', '00002'), 'GGUF'); + const pack = join(dataFolder, 'packs', 'ud_q4_k_xl'), mtp = join(dataFolder, 'mtp', 'rt'); + mkdirSync(join(pack, 'tokenizer'), { recursive: true }); + writeFileSync(join(pack, 'native_experts.txt'), 'x'); + const configPath = join(install, 'strata-ud_q4_k_xl.json'); + const args = ['--pack', pack, '--native', gguf, '--ple-gguf', gguf.replace('00001', '00002'), '--mtp', mtp, '--max-context', '131072', '--kv', 'int8']; + writeFileSync(configPath, JSON.stringify({ exe: 'component:strata-engine', tokenizer: join(pack, 'tokenizer'), args, model_name: 'qwen3.8-flash-next-ud_q4_k_xl', port: 8080 })); + writeFileSync(process.env.STRATA_SETTINGS_FILE, JSON.stringify({ data_dir: join(root, 'host-owned', 'Strata-data'), data_dir_at: 1700000000 })); + const state = { installs: 0, planCalls: [], rpc: [], starts: 0 }; + const control = { + control: { dir: join(root, 'control'), python: join(root, 'control', 'python', 'python') }, version: '0.2.10', + async installedModels() { + return { install_dir: install, models: [{ id: 'ud_q4_k_xl', name: 'qwen3.8-flash-next-ud_q4_k_xl', source_config: configPath, context_tokens: 131072, backend: 'cuda', component_status: 'present', missing: [], issue_codes: [] }] }; + }, + async engine() { return { usable: true, backend: 'cuda', components_dir: join(root, 'c'), engine: { version: '0.1.60', platform: 'linux-x86_64', backend: 'cuda', exe: join(root, 'engine', 'strata'), root: join(root, 'engine') } }; }, + async draftingPlan(options) { state.planCalls.push(options); return plan(options, state); }, + async draftingFetch() { return { ok: true, event: 'result', pack: null, skipped: false }; }, + async splitEngine() { return { ok: true, split: false, line: '', warn: false, reasons: [], written: null }; }, + async saveProfile() { return {}; }, + async start() { state.starts += 1; return { ok: true, state: 'ready' }; }, + async stop() { return { ok: true }; }, + }; + const catalog = { families: [{ family: 'qwen', title: 'Qwen3.8-Flash-Next', about: 'x', images: false, experimental: false, sizes: [ + { id: 'ud_q4_k_xl', model: 'UD-Q4_K_XL', download_gb: 22, ram_needed_gb: 60, about: 'x', on_this_pc: 'fits', installed: true, images: false, experimental: false }] }], + contexts: [32768, 131072], recommendation: { family: 'qwen', model: 'UD-Q4_K_XL', context: 131072 }, context_note: '' }; + const hardware = { ram_gb: 256, cpu: { name: 'cpu', cores: 32 }, engine_vendor: 'nvidia', gpus: [{ index: 0, name: 'NVIDIA GeForce RTX 3060', vendor: 'nvidia', vram_gb: 12, arch: 86, usable: true }] }; + const spawnProcess = (command, argv) => { + if (!String(argv[0]).endsWith('strata_mcp.py')) throw new Error('no process but the installer may be started in packaged mode: ' + command + ' ' + argv.join(' ')); + const child = new EventEmitter(); child.stdout = new EventEmitter(); child.stderr = new EventEmitter(); child.exitCode = null; + child.kill = () => { child.exitCode = 0; queueMicrotask(() => child.emit('exit', 0)); return true; }; + child.stdin = { write(line) { + const message = JSON.parse(line); if (!message.id) return; + if (message.method === 'tools/call') state.rpc.push(message.params); + let result; + if (message.method === 'initialize') result = { protocolVersion: '2024-11-05' }; + else if (message.method === 'tools/list') result = { tools: ['strata_models', 'strata_status', 'strata_install', 'strata_remove'].map(name => ({ name })) }; + else if (message.params.name === 'strata_models') result = { structuredContent: catalog }; + else if (message.params.name === 'strata_status') result = { structuredContent: { hardware, recommendation: catalog.recommendation, install: null, running: [], summary: 'x' } }; + else if (Object.keys(message.params.arguments ?? {}).length === 0) result = { structuredContent: { summary: 'Install finished', install: { running: false, exit_code: 0 } } }; + else if (message.params.arguments?.confirm) { state.installs += 1; result = { structuredContent: { summary: 'Install started', plan: {}, install: { running: true, step: '1/7' } } }; } // offline: the layer never arrives + else result = { structuredContent: { summary: 'Plan', plan: { title: 'Qwen3.8-Flash-Next UD-Q4_K_XL', download_gb: 5.2, disk_short: null } } }; + queueMicrotask(() => child.stdout.emit('data', Buffer.from(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + String.fromCharCode(10)))); + } }; + return child; + }; + const fetcher = async url => String(url).endsWith('/metrics') ? Response.json({ engine: {} }) : Response.json({ status: 'ok' }); + const native = createNativeControl({ control, directory: data, spawnProcess, fetcher, requests: () => [], modelsDirectory: () => modelsDir, hardwareProbe: () => [{ index: 0, vram_gb: 12 }] }); + return { native, state, modelFolder, mtp, configPath, + cleanup: () => { if (earlier === undefined) delete process.env.STRATA_INSTALL_DIR; else process.env.STRATA_INSTALL_DIR = earlier; rmSync(root, { recursive: true, force: true }); } }; +} +const finish = async (native, id) => { for (let i = 0; i < 1500; i++) { const job = native.state().jobs.find(j => j.id === id); if (job.state !== 'running') return job; await new Promise(r => setTimeout(r, 10)); } throw new Error('job did not finish'); }; +const installs = f => f.state.rpc.filter(p => p.name === 'strata_install' && p.arguments?.confirm === true); +const atInstall = choice => (options) => (!options.configPath ? { ok: true, serves: true, split: false, choice, drafter: choice, needed: false, mtp_needed: false, gb: 0 } : + { ok: true, serves: true, split: false, choice, drafter: choice, needed: false, mtp_needed: false, gb: 0, line: 'x' }); + + +const qwen4Install = async (f, extra) => finish(f.native, f.native.operation('install', { family: 'qwen4-35b-a3b', model: 'UD_Q4_K_XL', gguf_dir: f.modelFolder, ...extra }).id); + +test('(b) a re-prepare with Loop On passes --loop on to setup; Off or nothing passes no loop word', async () => { + for (const [given, want] of [['on', 'on'], ['off', undefined], [undefined, undefined]]) { + const f = fixture({ plan: atInstall('mtp') }); + try { + await f.native.refresh(true); + const job = await qwen4Install(f, given === undefined ? {} : { loop: given }); + assert.equal(job.state, 'done', job.error); + assert.equal(installs(f)[0].arguments.loop, want, `loop ${given}`); + } finally { await f.native.close(); f.cleanup(); } + } +}); + +test('(b) a context, KV or pictures change on a model whose saved Loop is On still carries loop on (the page sends it from the synced select)', async () => { + for (const change of [{ context: 65536 }, { kv: 'k8v4' }, { vision: 'no' }]) { + const f = fixture({ plan: atInstall('mtp') }); + try { + await f.native.refresh(true); + const job = await qwen4Install(f, { ...change, loop: 'on' }); + assert.equal(job.state, 'done', job.error); + assert.equal(installs(f)[0].arguments.loop, 'on', JSON.stringify(change)); + } finally { await f.native.close(); f.cleanup(); } + } +}); + +test('(b) another family never gets a loop word', async () => { + const f = fixture({ plan: atInstall('mtp') }); + try { + await f.native.refresh(true); + const job = await finish(f.native, f.native.operation('install', { family: 'qwen', model: 'UD_Q4_K_XL', gguf_dir: f.modelFolder, loop: 'on' }).id); + assert.equal(job.state, 'done', job.error); + assert.equal(installs(f)[0].arguments.loop, undefined); + } finally { await f.native.close(); f.cleanup(); } +}); + +const card = readFileSync(join(import.meta.dirname, '../src/components/NativeModels.tsx'), 'utf8'); + +test('(a) the Loop select starts from the saved value and follows it', () => { + assert.match(card, /setQ4Loop\(installed\.q4Loop === 'on' \? 'on' : 'off'\);/, 'an installed model: the saved Loop'); + assert.match(card, /setQ4Loop\('off'\);\s*\/\/ a model that is not installed/, 'a model that is not installed: Off, never the last model\'s'); + assert.match(card, /installed\?\.drafting, installed\?\.q4Loop, installed\?\.resourceProfile\]\)/, 'the effect follows a saved Loop that changes (a Load or a Settings save wrote it)'); +}); + +test('(a) what the page sends: the Load only when the choice differs from the saved one, the download with loop on only when On', () => { + assert.match(card, /\.\.\.\(q4LoopReady && q4Loop !== \(installed\?\.q4Loop \?\? 'off'\) \? \{ q4_loop: q4Loop \} : \{\}\)/); + assert.match(card, /\.\.\.\(q4LoopReady && q4Loop === 'on' \? \{ loop: 'on' \} : \{\}\)/); +});