okami.mom/strata

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

Niko1221Merge f-no-llama-server: the packaged app loads only through the Strata engine; a llama-server on PATH or in STRATA_LLAMA_SERVER is never started (Load and autostart)f5ef26889

main
6.0 KiB91 linesraw
1import './helpers/isolate-profile.mjs';   // a test run never writes the owner's real Strata profile
2/**
3 * The packaged app (STRATA_PACKAGED=1, STRATA_REAL_ENGINE=1, as the app shell starts the host) never starts a llama.cpp `llama-server`.
4 * 0.2.14 did: a GGUF imported through /api/local-models was loaded through a llama-server named by STRATA_LLAMA_SERVER (POST /load), and at
5 * the next start the host loaded it again by itself, also from a llama-server found on PATH, with nothing clicked. A fake llama-server that
6 * writes a marker line stands in both places; neither the load route nor the start-up path may ever run it.
7 */
8import test from 'node:test';
9import assert from 'node:assert/strict';
10import { spawn } from 'node:child_process';
11import { chmodSync, existsSync, mkdirSync, mkdtempSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
12import { createServer } from 'node:net';
13import { delimiter, join } from 'node:path';
14import { tmpdir } from 'node:os';
15import { fileURLToPath } from 'node:url';
16import { ggufFixture } from './helpers/local-gguf.mjs';
17
18const root = fileURLToPath(new URL('..', import.meta.url));
19const delay = ms => new Promise(r => setTimeout(r, ms));
20async function freePort() { const s = createServer(); await new Promise(r => s.listen(0, '127.0.0.1', r)); const p = s.address().port; await new Promise(r => s.close(r)); return p; }
21
22/** A llama-server look-alike: one marker line per start, answers /health like the real one, gone after 20 s. */
23function fakeServer(dir, marker) {
24  const js = join(dir, 'fake-llama-server.cjs');
25  writeFileSync(js, `const fs=require('fs'),http=require('http');
26fs.appendFileSync(${JSON.stringify(marker)}, JSON.stringify({pid:process.pid,argv:process.argv.slice(2)})+'\\n');
27const i=process.argv.indexOf('--port');const port=i>0?Number(process.argv[i+1]):8080;
28http.createServer((q,s)=>{s.setHeader('content-type','application/json');s.end(q.url.startsWith('/health')?'{"status":"ok"}':'{}');}).listen(port,'127.0.0.1');
29setTimeout(()=>process.exit(0),20000);
30`);
31  const unix = join(dir, 'llama-server');
32  writeFileSync(unix, `#!${process.execPath}\nrequire(${JSON.stringify(js)});\n`);
33  chmodSync(unix, 0o755);
34  writeFileSync(join(dir, 'llama-server.cmd'), `@"${process.execPath}" "${js}" %*\r\n`);
35  return process.platform === 'win32' ? join(dir, 'llama-server.cmd') : unix;
36}
37
38async function scenario(t, where) {
39  const home = mkdtempSync(join(tmpdir(), `strata-no-llama-${where}-`));
40  t.after(() => rmSync(home, { recursive: true, force: true }));
41  const bin = join(home, 'bin'), marker = join(home, 'marker.jsonl');
42  for (const d of ['bin', 'src', 'ws', 'components']) mkdirSync(join(home, d), { recursive: true });
43  const fake = fakeServer(bin, marker);
44  const gguf = join(home, 'src', 'Small-Q4_K_M.gguf');
45  ggufFixture(gguf, { arch: 'qwen3', name: 'no-llama fixture' });
46  const port = await freePort();
47  const env = { ...process.env, STRATA_PACKAGED: '1', STRATA_REAL_ENGINE: '1', NODE_ENV: 'production', STRATA_HOME: join(home, 'strata'),
48    STRATA_MODELS_DIR: join(home, 'strata', 'models'), STRATA_WORKSPACE: join(home, 'ws'), STRATA_COMPONENTS_DIR: join(home, 'components'),
49    STRATA_PORT: String(port), STRATA_SHARE_PORT: String(await freePort()), STRATA_TUNNEL_PORT: String(await freePort()) };
50  delete env.STRATA_LLAMA_SERVER; delete env.STRATA_API_KEY;
51  if (where === 'env') env.STRATA_LLAMA_SERVER = fake;
52  else { const key = Object.keys(env).find(k => k.toUpperCase() === 'PATH') ?? 'PATH'; env[key] = bin + delimiter + (env[key] ?? ''); }
53  const base = `http://127.0.0.1:${port}`;
54  const call = async (path, method = 'GET', body) => {
55    const r = await fetch(base + path, { method, headers: { 'content-type': 'application/json' }, body: body === undefined ? undefined : JSON.stringify(body) });
56    return { status: r.status, body: await r.json().catch(() => null) };
57  };
58  const starts = () => existsSync(marker) ? readFileSync(marker, 'utf8').split('\n').filter(Boolean).map(l => JSON.parse(l)) : [];
59  let host = null;
60  const up = async () => {
61    const log = openSync(join(home, 'host.log'), 'a');
62    host = spawn(process.execPath, ['server/index.mjs'], { cwd: root, env, stdio: ['ignore', log, log] });
63    for (let i = 0; i < 150; i++) { try { if ((await call('/api/health')).status === 200) return; } catch { /* not up yet */ } await delay(100); }
64    assert.fail('the host did not start: ' + readFileSync(join(home, 'host.log'), 'utf8').slice(-600));
65  };
66  const down = async () => { if (!host) return; const h = host; host = null; h.kill(); await Promise.race([new Promise(r => h.once('exit', r)), delay(8000)]); };
67  t.after(async () => { await down(); for (const s of starts()) { try { process.kill(s.pid); } catch { /* gone */ } } });
68
69  await up();
70  const imported = await call('/api/local-models', 'POST', { path: gguf });
71  assert.equal(imported.status, 200, JSON.stringify(imported.body));
72  const id = imported.body.variantId;
73  const load = await call(`/api/models/${encodeURIComponent(id)}/load`, 'POST');
74  await delay(1500);
75  assert.equal(load.status, 409, JSON.stringify(load.body));
76  assert.match(load.body?.error ?? '', /own engine/);
77  assert.deepEqual(starts(), [], 'POST /load started the llama-server');
78  assert.equal((await call('/api/engine/status')).body?.running, false);
79
80  await down();
81  await up();
82  await delay(2500);   // (0.2.14 began its start-up load 0.7 s after the host was up)
83  assert.deepEqual(starts(), [], 'the host start started the llama-server');
84  const library = (await call('/api/models')).body;
85  assert.equal(library?.loaded ?? null, null);
86  assert.notEqual(library?.current, id, 'an imported GGUF that never loaded became the remembered model');
87  assert.equal((await call('/api/engine/status')).body?.running, false);
88}
89
90test('the packaged host never starts a llama-server named by STRATA_LLAMA_SERVER (load route and host start)', { timeout: 90000 }, t => scenario(t, 'env'));
91test('the packaged host never starts a llama-server found on PATH (load route and host start)', { timeout: 90000 }, t => scenario(t, 'path'));