okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0213-int 3c4afa8d7 (Models page: no Connect engine / Download & load flash after a start) into f-0211-rc3fda37bd8

main
5.4 KiB89 linesraw
1/**
2 * QAGUI 0213 #2 (second part): while Strata Control starts slowly, /api/native must say "not known yet" (checking), not "engine not
3 * connected, nothing installed", and a read that failed during the start is read again within seconds, not after 45 s.
4 */
5import './helpers/isolate-profile.mjs';
6import test from 'node:test';
7import assert from 'node:assert/strict';
8import { EventEmitter } from 'node:events';
9import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
10import { join } from 'node:path';
11import { tmpdir } from 'node:os';
12import { build } from 'esbuild';
13import { fileURLToPath } from 'node:url';
14import { createNativeControl } from '../server/native-control.mjs';
15
16const here = p => fileURLToPath(new URL(p, import.meta.url));
17const result = await build({ entryPoints: [here('../src/lib/native.ts')], bundle: true, format: 'esm', platform: 'node', write: false, logLevel: 'silent', alias: { react: here('./helpers/react-stub.mjs') }, loader: { '.json': 'json' } });
18const { installedListPending } = await import(`data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString('base64')}`);
19
20function slowControl({ failFirst }) {
21  const root = mkdtempSync(join(tmpdir(), 'strata-chk-'));
22  const install = join(root, 'install'), data = join(root, 'data');
23  mkdirSync(install, { recursive: true }); mkdirSync(data, { recursive: true });
24  const configPath = join(install, 'strata-iq3_s.json');
25  writeFileSync(configPath, JSON.stringify({ exe: 'component:strata-engine', args: [], model_name: 'm', port: 8080 }));
26  let asked = 0;
27  const control = {
28    control: { dir: join(root, 'control'), python: join(root, 'control', 'python', 'python') }, version: '0.2.10',
29    async installedModels() {
30      asked++;
31      if (asked <= failFirst) throw new Error('Strata Control is still starting');
32      return { install_dir: install, models: [{ id: 'iq3_s', name: 'm', source_config: configPath, context_tokens: 8192, backend: 'cuda', component_status: 'present', missing: [], issue_codes: [] }] };
33    },
34    async engine() { return { usable: true, backend: 'cuda', components_dir: join(root, 'c'), engine: { version: '0.1.56', platform: 'linux-x86_64', backend: 'cuda', exe: join(root, 'e', 'strata'), root: join(root, 'e') } }; },
35  };
36  const catalog = { families: [{ family: 'qwen', title: 'Q', about: 'x', images: false, experimental: false, sizes: [{ id: 'iq3_s', model: 'IQ3_S', download_gb: 1, ram_needed_gb: 1, about: 'x', on_this_pc: 'fits', installed: true }] }], contexts: [8192], recommendation: { family: 'qwen', model: 'IQ3_S' }, context_note: '' };
37  const spawnProcess = () => {
38    const child = new EventEmitter(); child.stdout = new EventEmitter(); child.stderr = new EventEmitter(); child.exitCode = null;
39    child.kill = () => { child.exitCode = 0; return true; };
40    child.stdin = { write(line) {
41      const m = JSON.parse(line); if (!m.id) return;
42      const result = m.method === 'initialize' ? { protocolVersion: '2024-11-05' }
43        : m.method === 'tools/list' ? { tools: ['strata_models', 'strata_status'].map(name => ({ name })) }
44        : m.params.name === 'strata_models' ? { structuredContent: catalog } : { structuredContent: { hardware: { ram_gb: 16, gpus: [] }, install: null, running: [], summary: 'x' } };
45      queueMicrotask(() => child.stdout.emit('data', Buffer.from(JSON.stringify({ jsonrpc: '2.0', id: m.id, result }) + '\n')));
46    } };
47    return child;
48  };
49  const native = createNativeControl({ control, directory: data, spawnProcess, fetcher: async () => Response.json({ status: 'ok' }), requests: () => [], modelsDirectory: () => join(root, 'models'), hardwareProbe: () => [] });
50  return { native, asked: () => asked, cleanup: () => rmSync(root, { recursive: true, force: true }) };
51}
52
53test('a read that fails while Control starts is "checking", not "nothing installed", and the page waits on it', async () => {
54  const f = slowControl({ failFirst: 1 });
55  try {
56    const first = await f.native.refresh(false);
57    assert.equal(first.available, false);
58    assert.equal(first.checking, true, 'not known yet');
59    assert.equal(installedListPending(first), true, 'the page shows Checking, no Connect engine, no Download & load');
60    const again = await f.native.refresh(true);   // the next poll reads Control again (retry is soon, not after 45 s)
61    assert.equal(again.available, true);
62    assert.equal(again.checking, false);
63    assert.equal(installedListPending(again), false);
64    assert.equal(again.installed.models[0].ready, true, 'the downloaded model is shown as ready');
65  } finally { f.cleanup(); }
66});
67
68test('before the first answer the state is checking; a plain poll after a failed start is not held for 45 s', async () => {
69  const f = slowControl({ failFirst: 1 });
70  try {
71    assert.equal(f.native.state().checking, true);
72    await f.native.refresh(false);
73    await new Promise(r => setTimeout(r, 3100));
74    const next = await f.native.refresh(false);   // an ordinary (not forced) poll, a few seconds later
75    assert.equal(next.available, true);
76    assert.equal(next.checking, false);
77  } finally { f.cleanup(); }
78});
79
80test('a Control that keeps failing stops being "checking" so the real error shows', async () => {
81  const f = slowControl({ failFirst: 99 });
82  try {
83    let s;
84    for (let i = 0; i < 4; i++) s = await f.native.refresh(true);
85    assert.equal(s.checking, false);
86    assert.match(s.error, /still starting/);
87    assert.equal(installedListPending(s), false);
88  } finally { f.cleanup(); }
89});